import sys input = sys.stdin.readline N, Q = map(int, input().split()) S = list(input()) h = S.count("D") w = N - h T1 = [0] * 2 * N T2 = [0] * 2 * N Ac1 = [0] * (2 * N + 1) Ac2 = [0] * (2 * N + 1) for i in range(N): if S[i] == "D": T1[i] += 1 T1[i + N] += 1 else: T2[i] += 1 T2[i + N] += 1 for i in range(2 * 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 check(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(check, 2 * N, P)%N)