#include #include #include #define rep(i, n) for(i = 0; i < n; i++) #define int long long using namespace std; int n, q; string s; int cntR[400001], cntD[400001]; int solve(int h, int w, int p) { int ng = 0, ok = 1e+10, mid; while (ok - ng >= 2) { mid = (ng + ok) / 2; int cR = (mid / n) * cntR[n] + cntR[p + mid % n] - cntR[p]; int cD = (mid / n) * cntD[n] + cntD[p + mid % n] - cntD[p]; if (cR >= w || cD >= h) { ok = mid; } else { ng = mid; } } return (p + ok) % n; } signed main() { int i; cin >> n >> q >> s; rep(i, 2 * n) cntR[i + 1] = cntR[i] + (s[i % n] == 'R'); rep(i, 2 * n) cntD[i + 1] = cntD[i] + (s[i % n] == 'D'); rep(i, q) { int h, w, p; cin >> h >> w >> p; int ans = solve(h, w, p); cout << ans << endl; } return 0; }