#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, q;
  cin >> n >> q;
  long long root = (n + 1) / 2;
  for (int i = 0; i < q; ++i) {
    unsigned long long v;
    cin >> v;
    string s;
    cin >> s;
    n = s.size();
    for (int j = 0; j < n; j++) {
      if ((s[j] == 'L' || s[j] == 'R') && v % 2 == 1) continue;
      if (s[j] == 'U' && v == root) continue;
      long long st = __builtin_ctzll(v);
      if (s[j] == 'U') {
        if (v & ((long long)1 << (st + 1))) {
          v -= ((long long)1 << st);
        } else {
          v += ((long long)1 << st);
        }
      } else if (s[j] == 'R') {
        v += ((long long)1 << (st - 1));
      } else {
        v -= ((long long)1 << (st - 1));
      }
    }
    cout << v << endl;
  }
}
