#include <bits/stdc++.h>
using namespace std;
const int D = 70;
long long v[D], sz[D];
int top = 0;
stack<long long> R;
inline void go_up() {
  if (top == 1) return;
  --top;
  if (R.top() == v[top - 1]) R.pop();
}
inline void go_left() {
  if (sz[top - 1] == 1) return;
  sz[top] = (sz[top - 1] - 1) / 2;
  v[top] = R.top() + (sz[top] - 1) / 2 + 1;
  ++top;
}
inline void go_right() {
  if (sz[top - 1] == 1) return;
  R.push(v[top - 1]);
  sz[top] = (sz[top - 1] - 1) / 2;
  v[top] = R.top() + (sz[top] - 1) / 2 + 1;
  ++top;
}
int main() {
  ios::sync_with_stdio(false);
  long long n, q;
  cin >> n >> q;
  R.push(0);
  while (q--) {
    long long u;
    string s;
    cin >> u >> s;
    top = 1;
    v[0] = (n + 1) / 2;
    sz[0] = n;
    while (v[top - 1] != u) {
      if (u < v[top - 1])
        go_left();
      else
        go_right();
    }
    for (int i = (0), i_ = (s.size()); i < i_; ++i) {
      switch (s[i]) {
        case 'U':
          go_up();
          break;
        case 'L':
          go_left();
          break;
        case 'R':
          go_right();
          break;
      }
    }
    cout << v[top - 1] << endl;
    while ((int)R.size() > 1) R.pop();
  }
  return 0;
}
