#include using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; template using V = vector; template using VV = V>; template using VVV = V>; template using VVVV = VV>; #define rep(i,n) for(ll i=0ll;i void chmin(T& t, const U& u) { if (t > u) t = u; } template void chmax(T& t, const U& u) { if (t < u) t = u; } // cin.tie(nullptr); // ios::sync_with_stdio(false); // cout << fixed << setprecision(20); void solve(){ ll n,q; string s; cin >> n >> q >> s; V x(n+1, 0), y(n+1, 0); rep(i, n){ x[i+1] = x[i]; y[i+1] = y[i]; if(s[i] == 'D') x[i+1]++; else y[i+1]++; } rep(i, q){ ll h,w,p; cin >> h >> w >> p; h += x[p]; w += y[p]; p = 0; // cout << h << " " << w << endl; if(h >= x[n] && w >= y[n]){ if(x[n] == 0) w %= y[n]; else if(y[n] == 0) h %= x[n]; else{ ll m = min((h-1)/x[n], (w-1)/y[n]); h -= m*x[n]; w -= m*y[n]; } } ll ans = min(lower_bound(be(x), h) - x.begin(), lower_bound(be(y), w) - y.begin()); if(ans == n) ans = 0; cout << ans << endl; } } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int t=1; // cin >> t; rep(i,t) solve(); }