#include using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n, q; string s; cin >> n >> q >> s; vector dd(n+1), rr(n+1); for(int i = 0; i < n; i++){ if(s[i] == 'D') dd[i+1] += dd[i] + 1; else dd[i+1] = dd[i]; if(s[i] == 'R') rr[i+1] += rr[i] + 1; else rr[i+1] = rr[i]; } while(q--){ ll h, w, p, r, l = 0; cin >> h >> w >> p; r = h + w; while(r - l > 1){ ll m = (r + l) / 2, cd = 0, cr = 0; cd += dd[n] * ((p + m) / n) + dd[(p + m) % n]; cd -= dd[n] * (p / n) + dd[p % n]; cr += rr[n] * ((p + m) / n) + rr[(p + m) % n]; cr -= rr[n] * (p / n) + rr[p % n]; if(cd >= h || cr >= w) r = m; else l = m; } cout << (p + r) % n << '\n'; } return 0; }