#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 5;
const long long INF = 1e9 + 7;
long long n, x, Q, maxDepth;
string s;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> Q;
  long long root = (n + 1) >> 1, temp = 0;
  for (long long i = 0; i <= 64; i++) {
    temp += 1LL << i;
    if (temp == n) {
      maxDepth = i;
      break;
    }
  }
  while (Q--) {
    cin >> x >> s;
    long long mask = x;
    for (long long i = 0; i < s.size(); i++) {
      char c = s[i];
      long long newMask = mask & -mask;
      long long dep = __builtin_ctzll(mask);
      if (c == 'U' && mask != root) {
        if (mask >> (dep + 1) & 1)
          mask -= newMask;
        else
          mask += newMask;
      }
      if (c == 'L') {
        if (dep) mask -= newMask >> 1;
      }
      if (c == 'R') {
        if (dep) mask += newMask >> 1;
      }
    }
    cout << mask << "\n";
  }
  return 0;
}
