#include #include #include #include #include #include using namespace std; using namespace atcoder; using ll = long long; using mint = modint998244353; using vi = vector; using vvi = vector; using vvvi = vector; using vll = vector; using vvll = vector; using vvvll = vector; using vmi = vector; using vvmi = vector; using vvvmi = vector; #define all(a) (a).begin(), (a).end() #define rep2(i, m, n) for (int i = (m); i < (n); ++i) #define rep(i, n) rep2(i, 0, n) #define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i) #define drep(i, n) drep2(i, n, 0) void solve(){ } int main(){ int n, q; cin >> n >> q; string s; cin >> s; vvi pos(2); rep(i, s.length()){ int c = (s[i] == 'D') ? 0 : 1; pos[c].push_back(i); } while(q--){ int h, w, p; cin >> h >> w >> p; if(pos[0].size() == 0){ cout << (p+w)%n << endl; continue; } if(pos[1].size() == 0){ cout << (p+h)%n << endl; continue; } int zh = pos[0].size() - (lower_bound(all(pos[0]), p) - begin(pos[0])), zw = pos[1].size() - (lower_bound(all(pos[1]), p) - begin(pos[1])); if(zh >= h && zw >= w){ auto ith = lower_bound(all(pos[0]), p), itw = lower_bound(all(pos[1]), p); ith += h-1; itw += w-1; cout << (min(*ith, *itw) + 1)%n << endl; }else if(zh >= h){ auto ith = lower_bound(all(pos[0]), p); ith += h-1; cout << ((*ith) + 1)% n << endl; }else if(zw >= w){ auto itw = lower_bound(all(pos[1]), p); itw += w-1; cout << ((*itw) + 1)% n << endl; }else{ h -= zh; w -= zw; int lh = (h + pos[0].size() - 1)/pos[0].size(), lw = (w+pos[1].size() - 1)/pos[1].size(); if(lh > lw){ auto itw = pos[1].begin(); itw += ((w-1)%pos[1].size()); cout << ((*itw) + 1)% n << endl; }else if(lh < lw){ auto ith = pos[0].begin(); ith+= ((h-1)%pos[0].size()); cout << ((*ith) + 1)% n << endl; }else{ auto ith = pos[0].begin(); auto itw = pos[1].begin(); ith+= (((h-1)%pos[0].size())); itw += (((w-1)%pos[1].size())); cout << (min(*ith, *itw) + 1)%n << endl; } } } return 0; }