#include <bits/stdc++.h>
using namespace std;
long long t = 1;
long long bitshift(long long x) {
  long long ans = 1;
  for (long long i = 0; i < x; i += 1) ans *= 2;
  return ans;
}
long long height(long long n) {
  long long ans = 0;
  for (long long i = 0; i < 60; i += 1)
    if (bitshift(i) == n + 1) return i;
}
long long gdp(long long x) {
  long long ans = 0;
  for (long long i = 0; i < 60; i += 1) {
    if (x % bitshift(i) == 0)
      ans = i;
    else
      return ans;
  }
  return ans;
}
void solve() {
  long long n, x, q, ans, x1, x2, h;
  string s;
  cin >> n >> q;
  h = height(n);
  while (q--) {
    cin >> x >> s;
    ans = x;
    for (long long i = 0; i < int((s).size()); i += 1) {
      if (s[i] == 'U') {
        if (x == bitshift(h) / 2) continue;
        x1 = x + bitshift(gdp(x));
        x2 = x - bitshift(gdp(x));
        if (x2 <= 0)
          ans = x1;
        else {
          if (x1 % (bitshift(gdp(x) + 1)) == 0 &&
              x1 % (bitshift(2 + gdp(x))) != 0 && x1 <= n)
            ans = x1;
          else
            ans = x2;
        }
      }
      if (s[i] == 'L') {
        if (gdp(x) == 0) {
          continue;
        } else
          ans = x - (bitshift(gdp(x) - 1));
      }
      if (s[i] == 'R') {
        if (gdp(x) == 0)
          continue;
        else
          ans = x + (bitshift(gdp(x) - 1));
      }
      if (ans >= 1 && ans <= n) x = ans;
    }
    cout << x << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  while (t--) solve();
  return 0;
}
