#include using namespace std; using ll = long long; int main () { int N, Q; cin >> N >> Q; string s; cin >> s; std::vector sum[2]; string key = "DR"; for (int i = 0; i < 2; i ++) { sum[i].resize(N + 1); sum[i][0] = 0; for (int j = 0; j < N; j ++) { sum[i][j + 1] = sum[i][j] + (key[i] == s[j]); } } while (Q--) { int h, w, p; cin >> h >> w >> p; ll Ls[] = {h, w}; ll ans = (ll)1e18; for (int q = 0; q < 2; q ++) { if (sum[q][N] - sum[q][p] >= Ls[q]) { auto pt = lower_bound(sum[q].begin(), sum[q].end(), Ls[q] + sum[q][p]); ll d = pt - sum[q].begin(); d -= p; ans = min(ans, d); } else { ll goal = Ls[q] - sum[q][p]; ll pl = N - p; pl += ((goal - 1) / sum[q][N]) * N; goal = (goal - 1) % sum[q][N] + 1; auto pt = lower_bound(sum[q].begin(), sum[q].end(), Ls[q]); pl += pt - sum[q].begin(); ans = min(ans, pl); } } cout << (ans + p) % N << endl; } }