#include using namespace std; using ll = long long; const int INF = 1e9 + 10; const ll INFL = 4e18; int main() { int N, Q; cin >> N >> Q; string S; cin >> S; vector> cnt(N + 1, vector(2, 0)); for (int i = 0; i < N; i++) { cnt[i + 1][0] = cnt[i][0]; cnt[i + 1][1] = cnt[i][1]; if (S[i] == 'D') { cnt[i + 1][0]++; } else { cnt[i + 1][1]++; } } while (Q--) { ll H, W, P; cin >> H >> W >> P; ll lo = 0, hi = 1ll * INF * 3; while (hi - lo > 1) { ll mid = (lo + hi) / 2; ll cycle = mid / N; ll rem = (P + mid) % N; ll nx = 0, ny = 0; nx += cnt[N][0] * cycle; ny += cnt[N][1] * cycle; if (P <= rem) { nx += cnt[rem][0] - cnt[P][0]; ny += cnt[rem][1] - cnt[P][1]; } else { nx += cnt[N][0] - cnt[P][0] + cnt[rem][0]; ny += cnt[N][1] - cnt[P][1] + cnt[rem][1]; } if (nx < H && ny < W) { lo = mid; } else { hi = mid; } } cout << (P + hi) % N << '\n'; } }