#include using namespace std; using ll = long long; ll cc(int n, ll c, ll m, ll p, vector dr){ c += dr[n] * ((p + m) / n) + dr[(p + m) % n]; c -= dr[n] * (p / n) + dr[p % n]; return c; } 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 = cc(n, cd, m, p, dd); cr = cc(n, cr, m, p, rr); if(cd >= h || cr >= w) r = m; else l = m; } cout << (p + r) % n << '\n'; } return 0; }