#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long N = 5e5 + 1;
const long long mod = 1e9 + 7;
const long double eps = 1E-7;
long long x, y;
string s;
long long n, q, m, f;
vector<char> V;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> q;
  for (long long i = 1;; i++) {
    if ((1ll << i) > n) break;
    f = i;
  }
  while (q--) {
    cin >> m >> s;
    y = f;
    x = (1ll << f);
    while (x != m) {
      y--;
      if (x > m)
        V.push_back('L'), x -= (1ll << y);
      else
        V.push_back('R'), x += (1ll << y);
    }
    for (auto h : s) {
      if (h == 'U') {
        if (V.empty()) continue;
        if (V.back() == 'L') x += (1ll << y);
        if (V.back() == 'R') x -= (1ll << y);
        y++;
        V.pop_back();
      }
      if (h == 'L') {
        if (y == 0) continue;
        V.push_back(h);
        y--;
        x -= (1ll << y);
      }
      if (h == 'R') {
        if (y == 0) continue;
        V.push_back(h);
        y--;
        x += (1ll << y);
      }
    }
    V.clear();
    cout << x << endl;
  }
}
