#include using namespace std; #if __cplusplus < 202002L template int ssize(const T& a){ return a.size(); } #endif template istream& operator>> (istream& in, pair & a){ in >> a.first >> a.second; return in; } template istream& operator>> (istream& in, vector & a){ for (auto &x: a){ in >> x; } return in; } using ll = long long; using ld = long double; signed main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); if (fopen("KEK.inp", "r")){ freopen("KEK.inp", "r", stdin); freopen("KEK.out", "w", stdout); } int n, q; cin >> n >> q; string s; cin >> s; vector a, b; for (auto i = 0; i < n; i++){ if (s[i] == 'D'){ a.emplace_back(i); } else{ b.emplace_back(i); } } constexpr int inf = 1e9 + 7; while (q--){ int h, w, i; cin >> h >> w >> i; int ans = -1; auto jump = [&](){ int cnt_h = ssize(a) - (lower_bound(begin(a), end(a), i) - begin(a)); int cnt_w = ssize(b) - (lower_bound(begin(b), end(b), i) - begin(b)); if (h > cnt_h and w > cnt_w){ h -= cnt_h; w -= cnt_w; i = 0; return; } int pos = n; if (h <= cnt_h){ pos = min(pos, *(lower_bound(begin(a), end(a), i) + h - 1)); } if (w <= cnt_w){ pos = min(pos, *(lower_bound(begin(b), end(b), i) + w - 1)); } ans = pos + 1; if (ans >= n){ ans -= n; } }; jump(); if (ans == -1){ int loop = min(a.empty() ? inf : (h - 1) / ssize(a), b.empty() ? inf : (w - 1) / ssize(b)); h -= loop * ssize(a); w -= loop * ssize(b); jump(); } cout << ans << "\n"; } }