#include using namespace std; #define all(v) (v).begin(),(v).end() #define pb(a) push_back(a) #define rep(i, n) for(int i=0;i> n >> q; string s; cin >> s; vector vr, vd; rep(i, n) { if(s[i] == 'R') vr.pb(i); else vd.pb(i); } rep(_, q) { ll h, w, p; cin >> h >> w >> p; ll ans = INF; int id = -1; if((int)vr.size()) { ll idx = lower_bound(all(vr), p) - vr.begin(); if((ll)vr.size() - idx >= w) { ans = min(ans, vr[idx + w - 1] + 1); } else { w -= (ll)vr.size() - idx; ll num = w / (ll)vr.size(); ll r = w % (ll)vr.size(); if(r == 0) { ans = min(ans, n + (num - 1) * n + vr.back() + 1); } else { ans = min(ans, n + num * n + vr[r - 1] + 1); } } } if((int)vd.size()) { ll idx = lower_bound(all(vd), p) - vd.begin(); if((ll)vd.size() - idx >= h) { ans = min(ans, vd[idx + h - 1] + 1); } else { h -= (ll)vd.size() - idx; ll num = h / (ll)vd.size(); ll r = h % (ll)vd.size(); if(r == 0) { ans = min(ans, n + (num - 1) * n + vd.back() + 1); } else { ans = min(ans, n + num * n + vd[r - 1] + 1); } } } cout << ans % n << endl; } return 0; }