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 LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] 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)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = 1 << 63 # md = 998244353 md = 10**9+7 from bisect import bisect 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) cc = cntc(s) tc = cntc(t) itc = cntc(t[::-1]) ww = [x] aa = [1] bb = [0] cur = x while ww[-1] < 10**9+5: cur = cur*2+y ww.append(cur) aa.append(aa[-1]*2) bb.append(bb[-1]*2+1) # print(ww) # print(aa) # print(bb) @lru_cache(None) def solve(r, c): res = 0 si = bisect(ww, r)-1 rev = False for i in range(si, -1, -1): w = ww[i] if w > r: rev = False continue if w == r: res += aa[i]*cc[-1][c]+bb[i]*tc[-1][c] r = 0 break res += aa[i]*cc[-1][c]+bb[i]*tc[-1][c] if r-w <= y: if rev: res += itc[r-w][c] else: res += tc[r-w][c] r = 0 break else: res += tc[-1][c] r -= w+y rev = True res += cc[r][c] return res 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(r, c)-solve(l, c) print(ans)