#include <bits/stdc++.h>
using namespace std;
string s;
long long n, q, cnt, x;
long long cen(long long x) {
  long long k = cnt - 1;
  while (x != 1LL << k) {
    if (x > (1LL << k)) x -= 1LL << k;
    --k;
  }
  return k;
}
int main() {
  cin >> n >> q;
  long long tmp = n + 1;
  while (tmp >>= 1) ++cnt;
  while (q--) {
    cin >> x >> s;
    long long k = cen(x);
    for (int(i) = (int)(0); (i) < (int)(s.size()); ++(i)) {
      if (s[i] == 'U') {
        long long t = x + (1LL << k);
        if (t <= n && cen(t) == k + 1) {
          x = t;
          ++k;
        } else {
          t = x - (1LL << k);
          if (t > 0 && cen(t) == k + 1) {
            x = t;
            ++k;
          }
        }
      } else if (s[i] == 'R') {
        long long t = x + (1LL << (k - 1));
        if (t <= n && cen(t) == k - 1) {
          x = t;
          --k;
        }
      } else {
        long long t = x - (1LL << (k - 1));
        if (t > 0 && cen(t) == k - 1) {
          x = t;
          --k;
        }
      }
    }
    cout << x << endl;
  }
  return 0;
}
