#include #include using namespace std; using ll = long long; int N, Q; string S; int X[202020], Y[202020]; int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N >> Q >> S; for(int i = 0;i < N;i++){ X[i + 1] = X[i] + (S[i] == 'D'); Y[i + 1] = Y[i] + (S[i] == 'R'); } while(Q--){ int h, w, p; cin >> h >> w >> p; h += X[p], w += Y[p]; ll l = -1, r = 4e9; while(r - l > 1){ ll m = (l + r) / 2; bool f = false; f |= m / N * X[N] + X[m % N] >= h; f |= m / N * Y[N] + Y[m % N] >= w; if(f)r = m; else l = m; } cout << r % N << "\n"; } return 0; }