def sc(s): c=[] for i in range(26): c+=[[0]*(len(s)+1)] for j in range(len(s)): c[i][j+1]=s[j]==chr(i+ord("a")) c[i][j+1]+=c[i][j] return c X=input() xl=len(X) xc=sc(X) Y=input() yl=len(Y) yc=sc(Y) def solve(p): xy=p%(xl+yl)<=xl L=(xl+yl)*(1<<30)-yl f=1 while L>xl: if p<=(L-yl)//2: p=p elif p<=(L-yl)//2+yl: p-=(L-yl)//2 L=xl+yl+xl else: p=L+1-p f^=1 L=(L-yl)//2 return (xc[c][p] if f else xc[c][xl]-xc[c][p-1]) if xy else (yc[c][p] if f else yc[c][yl]-yc[c][p-1])+xc[c][xl] Q=int(input()) for _ in range(Q): l,r,c=input().split() l=int(l)-1 r=int(r) c=ord(c)-ord("a") a=0 a+=(xc[c][xl]+yc[c][yl])*(r//(xl+yl)) a+=solve(r) if r%(xl+yl)!=0 else 0 a-=(xc[c][xl]+yc[c][yl])*(l//(xl+yl)) a-=solve(l) if l%(xl+yl)!=0 else 0 print(a)