x = [ord(c) - 97 for c in input()] y = [ord(c) - 97 for c in input()] lx = len(x) ly = len(y) def get_acc(d): l = len(d) res1 = [[0] * (l + 1) for _ in range(26)] res2 = [[0] * (l + 1) for _ in range(26)] for i in range(l): for j in range(26): res1[j][i + 1] = res1[j][i] res1[d[i]][i + 1] += 1 for i in range(l)[::-1]: for j in range(26): res2[j][i] = res2[j][i + 1] res2[d[i]][i] += 1 return res1, res2 axl, axr = get_acc(x) ayl, ayr = get_acc(y) sz = [] l = lx cx = 1 cy = 0 while not sz or sz[-1][-1] < 10 ** 9: sz.append((l, cx, cy)) l = 2 * l + ly cx = 2 * cx cy = 2 * cy + 1 def f(r, c): flipped = False tx = ty = 0 for l, cx, cy in sz[::-1]: if r < l: if flipped: flipped = not flipped continue tx += cx ty += cy r -= l if r >= ly: ty += 1 r -= ly if not flipped: flipped = not flipped continue res = tx * axl[c][-1] + ty * ayl[c][-1] if flipped: res += ayr[c][ly - r] else: res += ayl[c][r] return res res = tx * axl[c][-1] + ty * ayl[c][-1] if flipped: res += axr[c][lx - r] else: res += axl[c][r] return res for _ in range(int(input())): l, r, c = input().split() c = ord(c) - 97 print(f(int(r), c) - f(int(l) - 1, c))