#include <bits/stdc++.h>
char s[100005], stack[100005], ss;
int main() {
  long long n, k, now, i, i2, l, plus = 0, kid, pos;
  scanf("%lld %lld", &n, &k);
  for (i = 1; i <= k; i++) {
    scanf("%lld", &now);
    plus = 0;
    ss = 0;
    kid = n;
    while (kid / 2 + plus + 1 != now) {
      if (kid / 2 + plus + 1 < now) {
        stack[++ss] = 'R';
        plus += kid / 2 + 1;
        kid /= 2;
      } else {
        stack[++ss] = 'L';
        kid /= 2;
      }
    }
    now = kid;
    scanf("%s", s);
    l = strlen(s);
    for (i2 = 0; i2 < l; i2++) {
      if (s[i2] == 'R') {
        if (now == 1) continue;
        plus += now / 2 + 1;
        now /= 2;
        stack[++ss] = 'R';
      } else if (s[i2] == 'L') {
        if (now == 1) continue;
        now /= 2;
        stack[++ss] = 'L';
      } else {
        if (now == n) continue;
        if (stack[ss] == 'R') plus -= now + 1;
        now = now * 2 + 1;
        ss--;
      }
    }
    printf("%lld\n", plus + now / 2 + 1);
  }
  return 0;
}
