#include <bits/stdc++.h>
using namespace std;
int bsf(int x) { return __builtin_ctz(x); }
int bsr(int x) { return 31 - __builtin_clz(x); }
char b[(int)1e+5];
int calc(long long x) {
  int ret = 0;
  for (; ((1ll << (long long)ret) ^ x) & (1ll << (long long)ret); ++ret)
    ;
  return ret;
}
int main() {
  long long n;
  int q;
  scanf("%lld%d", &n, &q);
  long long top = n / 2;
  ++top;
  for (int i = 0; i < q; ++i) {
    scanf("%lld%s", &n, b);
    for (char *a = b; *a; ++a) {
      if (*a == 'U') {
        if (n != top) {
          int h = calc(n);
          n ^= 1ll << (long long)h;
          n |= 1ll << (long long)(h + 1);
        }
      } else {
        int h = calc(n);
        if (h) {
          if (*a == 'L') {
            n ^= 1ll << (long long)h;
          }
          n |= 1ll << (long long)(h - 1);
        }
      }
    }
    printf("%lld\n", n);
  }
  return 0;
}
