#include <bits/stdc++.h>
using namespace std;
long long n, q, H;
string s;
long long getH(long long x) {
  int cnt = 0;
  while (!(x & 1)) {
    x >>= 1;
    cnt++;
  }
  return cnt - 1;
}
void moveL(long long &x) {
  if (x & 1) return;
  x = x - (1ll << getH(x));
}
void moveR(long long &x) {
  if (x & 1) return;
  x = x + (1ll << getH(x));
}
void moveU(long long &x) {
  if (x == n / 2 + 1) return;
  long long p = n / 2 + 1, ne, h = H - 2;
  while (1) {
    if (x < p)
      ne = p - (1ll << h);
    else
      ne = p + (1ll << h);
    if (ne == x) {
      x = p;
      return;
    }
    h--;
    p = ne;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;
  long long x = 1;
  while (x <= n) {
    x <<= 1;
    H++;
  }
  while (q--) {
    cin >> x;
    cin >> s;
    for (int i = 0; i < ((int)s.length()); i++) {
      if (s[i] == 'L') moveL(x);
      if (s[i] == 'R') moveR(x);
      if (s[i] == 'U') moveU(x);
    }
    cout << x << "\n";
  }
}
