import sys sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = 18446744073709551615 # inf = 4294967295 md = 10**9+7 # md = 998244353 from collections import Counter from functools import lru_cache def cntc(s): res = [] cnt = Counter() res.append(cnt.copy()) for c in s: c = ord(c)-97 cnt[c] += 1 res.append(cnt.copy()) return res s = SI() t = SI() x = len(s) y = len(t) sc = cntc(s) tc = cntc(t) isc = cntc(s[::-1]) itc = cntc(t[::-1]) ww = [x] aa = [1] bb = [0] while ww[-1] < 10**9+5: ww.append(ww[-1]*2+y) aa.append(aa[-1]*2) bb.append(bb[-1]*2+1) # print(ww) # print(aa) # print(bb) @lru_cache(None) def solve(lv, s, c, rev=False): if s == ww[lv]: return sc[-1][c]*aa[lv]+tc[-1][c]*bb[lv] if lv == 0: if rev: return isc[s][c] return sc[s][c] lw = ww[lv-1] if s <= lw: return solve(lv-1, s, c, False) left = sc[-1][c]*aa[lv-1]+tc[-1][c]*bb[lv-1] if s <= lw+y: if rev: return left+itc[s-lw][c] return left+tc[s-lw][c] if rev: left+itc[-1][c]+solve(lv-1, s-lw-y, c, True) return left+tc[-1][c]+solve(lv-1, s-lw-y, c, True) for _ in range(II()): l, r, c = SI().split() l = int(l)-1 r = int(r) c = ord(c)-97 # print(solve(r, c),solve(l, c)) ans = solve(len(ww)-1, r, c)-solve(len(ww)-1, l, c) print(ans)