#include <bits/stdc++.h>
const int N = 1e5;
long long n, m;
char s[N + 10];
inline int getBit(long long x, int pos) { return !((x & (1LL << pos)) == 0); }
inline void L(long long& x) {
  if (x & 1) return;
  int pos = 0;
  while (pos < 60 && getBit(x, pos) == 0) pos++;
  x ^= ((1LL << pos) | (1LL << (pos - 1)));
}
inline void R(long long& x) {
  if (x & 1) return;
  int pos = 0;
  while (pos < 60 && getBit(x, pos) == 0) pos++;
  x ^= (1LL << (pos - 1));
}
inline void U(long long& x) {
  int pos = 0;
  while (pos < 60 && getBit(x, pos) == 0) pos++;
  if (n + 1 == (1LL << (pos + 1))) return;
  if (getBit(x, pos + 1) == 1)
    x ^= (1LL << pos);
  else
    x ^= ((1LL << pos) | (1LL << (pos + 1)));
}
int main() {
  scanf("%lld%lld", &n, &m);
  while (m--) {
    int len;
    long long x;
    scanf("%lld%s", &x, s + 1);
    len = int(strlen(s + 1));
    for (int i = 1; i <= len; i++) {
      if (s[i] == 'U') U(x);
      if (s[i] == 'L') L(x);
      if (s[i] == 'R') R(x);
    }
    printf("%lld\n", x);
  }
  return 0;
}
