import java.io.*; import java.util.*; class Main { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); char a[]=bu.readLine().toCharArray(),b[]=bu.readLine().toCharArray(); int n=a.length,m=b.length; int ca[][][]=new int[2][n+1][26],cb[][][]=new int[2][m+1][26]; cumulative(a,ca); cumulative(b,cb); int q=Integer.parseInt(bu.readLine()); while(q-->0) { String s[]=bu.readLine().split(" "); int l=Integer.parseInt(s[0])-1,r=Integer.parseInt(s[1]),t=s[2].charAt(0)-'a'; long ans=count(ca,cb,r,t)-count(ca,cb,l,t); sb.append(ans+"\n"); } System.out.print(sb); } static void cumulative(char s[],int c[][][]) { int n=s.length,i; for(i=1;i<=n;i++) { //i characters from left int x=s[i-1]-'a',j; for(j=0;j<26;j++) c[0][i][j]=c[0][i-1][j]; c[0][i][x]++; //from right x=s[n-i]-'a'; for(j=0;j<26;j++) c[1][i][j]=c[1][i-1][j]; c[1][i][x]++; } } static long count(int a[][][],int b[][][],int x,int t) { if(x==0) return 0; int n=a[0].length-1,m=b[0].length-1; int times=x/(n+m); long ans=1l*times*a[0][n][t]+1l*times*b[0][m][t]; x-=times*(n+m); if(x<=n) { //L R L R format if(times%2==0) ans+=a[0][x][t]; else ans+=a[1][x][t]; } else { ans+=a[0][n][t]; x-=n; //how to find orientation of iteration in current move? int or=orientation(times+1); if(or==0) ans+=b[0][x][t]; else ans+=b[1][x][t]; } return ans; } static int orientation(int k) { //l llr llrllrr goes on and on //we need to find the value at ith index if(k==1) return 0; long p2=1; while(p2