#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5;
int main() {
  long long n, j = 0, l, k, q, v, vi, wl, ans[mx], m;
  char s[mx], s2[mx];
  cin >> n >> q;
  while (q) {
    k = 0;
    vi = n / 2 + 1;
    cin >> v >> s;
    wl = v;
    while (wl % 2 == 0) {
      wl /= 2;
      k++;
    }
    for (int i = 0; i < strlen(s); i++) {
      if (s[i] == 'U')
        if (v == vi)
          continue;
        else {
          wl = v + (long long)pow(2, k);
          k++;
          m = (long long)pow(2, k);
          if (wl % m == 0 && wl % (long long)(m * 2))
            v = wl;
          else
            v -= (long long)pow(2, k - 1);
        }
      if (s[i] == 'L' && k) k--, wl = (long long)pow(2, k), v -= wl;
      if (s[i] == 'R' && k) k--, wl = (long long)pow(2, k), v += wl;
    }
    ans[j++] = v;
    q--;
  }
  for (int i = 0; i < j; i++) cout << ans[i] << "\n";
}
