#include <bits/stdc++.h>
using namespace std;
const int maxn = 1;
long long n, root[60], p2[60], pos;
int q, len, h;
string st;
int lg(long long n) {
  if (n == 1) return 0;
  return lg(n / 2) + 1;
}
void solve() {
  while (q--) {
    cin >> pos >> st;
    root[0] = (n + 1LL) >> 1LL, len = 0, h = lg(root[0]);
    while (1) {
      if (root[len] == pos) break;
      ++len;
      if (pos < root[len - 1])
        --h, root[len] = root[len - 1] - p2[h];
      else
        --h, root[len] = root[len - 1] + p2[h];
    }
    for (auto v : st) {
      if ((h == 0 && v != 'U') || (len == 0 && v == 'U')) continue;
      if (v == 'U') --len, ++h;
      if (v == 'L') --h, ++len, root[len] = root[len - 1] - p2[h];
      if (v == 'R') --h, ++len, root[len] = root[len - 1] + p2[h];
    }
    cout << root[len] << '\n';
  }
}
void enter() {
  ios_base::sync_with_stdio(), cin.tie(0), cout.tie(0);
  cin >> n >> q;
  p2[0] = 1;
  for (int i = 1; i < 60; ++i) p2[i] = 2LL * p2[i - 1];
}
int main() {
  enter();
  solve();
}
