def H(C,N,K): if K in Memo: return Memo[K] if K==0: return 0 code=ord(C)-ord("a") if N==1: Memo[K]=X_cnt[code][K] else: if K<=Len[N-1]: Memo[K]=H(C,N-1,K) elif K<=Len[N-1]+Y_len: Memo[K]=E[code][N-1]+Y_cnt[code][K-Len[N-1]] else: Memo[K]=2*E[code][N-1]+Y_cnt[code][Y_len]-H(C,N-1,2*Len[N-1]+Y_len-K) return Memo[K] def count(S): M=[[0]*len(S) for _ in range(sigma)] for i in range(sigma): a=Z[i] m=M[i] for j in range(1,len(S)): m[j]=m[j-1]+(1 if S[j]==a else 0) return M #================================================= import sys from string import ascii_lowercase as Z input=sys.stdin.readline write=sys.stdout.write X="*"+input()[:-1]; X_len=len(X)-1 Y="*"+input()[:-1]; Y_len=len(Y)-1 Len=[0,X_len] while Len[-1]<=pow(10,18): Len.append(2*Len[-1]+Y_len) sigma=len(Z) X_cnt=count(X) Y_cnt=count(Y) N=len(Len)-1 E=[[0,X_cnt[a][-1]] for a in range(sigma)] for a in range(sigma): e=E[a] y=Y_cnt[a][Y_len] for _ in range(2,N+1): e.append(2*e[-1]+y) Q=int(input()) A=[0]*Q for case in range(Q): L,R,C=input().split() L=int(L); R=int(R) Memo={} A[case]=H(C,N,R)-H(C,N,L-1) write("\n".join(map(str,A)))