#include <bits/stdc++.h>
using namespace std;
template <class T>
T pmax(T a, T b) {
  return max(a, b);
}
const long long int mod = 1e9 + 7;
const long long int Maa = 2e5 + 3;
long long int n, q;
int main() {
  cin >> n >> q;
  long long int m = n + 1, root1, power;
  for (int i = 0; i < 64; i++) {
    if ((m >> i) & 1) {
      power = i - 1;
      break;
    }
  }
  root1 = pow(2, power);
  for (int i = 0; i < q; i++) {
    long long int u, root = root1, p = power;
    string s;
    cin >> u >> s;
    vector<long long int> v;
    while (root != u) {
      if (u > root)
        root = root + pow(2, --p), v.push_back(1);
      else if (u < root)
        root = root - pow(2, --p), v.push_back(0);
    }
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == 'L' and p - 1 >= 0)
        root = root - pow(2, --p), v.push_back(0);
      else if (s[j] == 'R' and p - 1 >= 0)
        root = root + pow(2, --p), v.push_back(1);
      else if (s[j] == 'U') {
        if (root != root1) {
          long long int b = v[v.size() - 1];
          v.pop_back();
          if (b == 1)
            root = root - pow(2, p);
          else if (b == 0)
            root = root + pow(2, p);
          p++;
        }
      }
    }
    cout << root << "\n";
  }
}
