#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  long long q;
  cin >> n >> q;
  long long d = __builtin_ctzll(n + 1);
  while (q--) {
    long long x;
    string p;
    cin >> x >> p;
    for (char c : p) {
      long long dx = __builtin_ctzll(x);
      if (c == 'U' && dx < d - 1) {
        long long x1 = x + (1ll << dx);
        long long x2 = x - (1ll << dx);
        if (x1 > n)
          x = x2;
        else if (__builtin_ctzll(x1) == dx + 1)
          x = x1;
        else
          x = x2;
      } else if (c == 'L' && dx > 0) {
        x -= 1ll << (dx - 1);
      } else if (c == 'R' && dx > 0) {
        x += 1ll << (dx - 1);
      }
    }
    cout << x << '\n';
  }
  return 0;
}
