import sys input = sys.stdin.readline N, Q = map(int, input().split()) S = list(input()) h = S.count("D") w = N - h T1 = [0] * 3 * N T2 = [0] * 3 * N Ac1 = [0] * (3 * N + 1) Ac2 = [0] * (3 * N + 1) for i in range(N): if S[i] == "D": T1[i] += 1 T1[i + N] += 1 T1[i + 2 * N] += 1 else: T2[i] += 1 T2[i + N] += 1 T2[i + 2 * N] += 1 for i in range(3 * N): Ac1[i + 1] = Ac1[i] + T1[i] Ac2[i + 1] = Ac2[i] + T2[i] def BinarySearch(check, yes = 10 ** 18, no = -1): while abs(yes - no) != 1: mid = (yes + no)//2 if check(mid): yes = mid else: no = mid return yes def check1(m): return (H - h * m) > 0 and (W - w * m) > 0 def check2(r): x = Ac1[r] - Ac1[P] y = Ac2[r] - Ac2[P] return H - x <= 0 or W - y <= 0 def div(x, y): if y == 0: return 10 ** 18 return x // y for _ in range(Q): H, W, P = map(int, input().split()) m = min(div(H - 1, h), div(W - 1, w)) H -= m * h W -= m * w print(BinarySearch(check2, 3 * N, P)%N)