#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e5 + 100;
char moves[mxn];
long long n, root;
int clz(long long mask) { return __builtin_clzll(mask); }
int ctz(long long mask) { return __builtin_ctzll(mask); }
long long off(long long mask) { return mask & (mask - 1); }
long long sbit(long long mask) { return mask & -mask; }
long long moveup(long long mask) {
  if (mask == root)
    return mask;
  else if (((mask >> ctz(mask)) & 3) == 3)
    return off(mask);
  else
    return mask ^ (sbit(mask) | (sbit(mask) << 1));
}
long long moveright(long long mask) {
  return mask & 1 ? mask : mask | (sbit(mask) >> 1);
}
long long moveleft(long long mask) {
  return mask & 1 ? mask : off(mask) | (sbit(mask) >> 1);
}
int main() {
  long long n, node;
  int q, m;
  scanf("%lld%d", &n, &q);
  root = (n + 1) >> 1;
  while (q--) {
    scanf("%lld", &node);
    scanf("%s", moves);
    m = strlen(moves);
    for (int i = 0; i < m; i++) {
      if (moves[i] == 'U')
        node = moveup(node);
      else if (moves[i] == 'L')
        node = moveleft(node);
      else
        node = moveright(node);
    }
    printf("%lld\n", node);
  }
}
