#include char s[200005]; long long int x[400005], y[400005]; int main() { long long int n, q; scanf("%lld %lld", &n, &q); scanf("%s", s); long long int i; x[0] = y[0] = 0; for (i = 0; i < n; i++) { x[i + 1] = x[i]; y[i + 1] = y[i]; if (s[i] == 'D') x[i + 1]++; else y[i + 1]++; } for (i = 0; i < n; i++) { x[i + 1 + n] = x[i + n]; y[i + 1 + n] = y[i + n]; if (s[i] == 'D') x[i + 1 + n]++; else y[i + 1 + n]++; } long long int min, mid, max; long long int h, w, p; long long int f1, f2, f; for (i = 0; i < q; i++) { scanf("%lld %lld %lld", &h, &w, &p); if (x[n] > 0) f1 = (h - 1) / x[n]; else f1 = 1e18; if (y[n] > 0) f2 = (w - 1) / y[n]; else f2 = 1e18; if (f1 < f2) f = f1; else f = f2; h -= x[n] * f; w -= y[n] * f; if (h > x[n] && w > y[n]) { h -= x[n]; w -= y[n]; } min = p; max = 2 * n; while (max - min > 1) { mid = (max + min) / 2; if (x[mid] - x[p] < h && y[mid] - y[p] < w) min = mid; else max = mid; } printf("%lld\n", max % n); } return 0; }