#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
char op[maxn];
int main() {
  long long n, q;
  while (scanf("%lld %lld", &n, &q) != EOF) {
    long long root = (n + 1) >> 1;
    long long u;
    while (q--) {
      scanf("%lld", &u);
      scanf("%s", op);
      int len = strlen(op);
      long long k, y, tmp;
      for (int i = 0; i < len; i++) {
        if (op[i] == 'L') {
          k = u & (-u);
          if (u % 2 == 0) u = u - (k >> 1);
        } else if (op[i] == 'R') {
          k = u & (-u);
          if (u % 2 == 0) u = u + (k >> 1);
        } else if (op[i] == 'U') {
          k = u & (-u);
          if (k != root) {
            tmp = u + k;
            if ((tmp % (k << 1)) == 0 && (tmp % (k << 2) != 0))
              u = tmp;
            else
              u = u - k;
          }
        }
      }
      printf("%lld\n", u);
    }
  }
  return 0;
}
