#include <bits/stdc++.h>
#pragma gcc optimize("unroll-loop")
#pragma gcc optimize("Ofast")
using namespace std;
const long long MOD = 1e9 + 7;
const long long N = 2e5 + 5;
long long gcd(long long n, long long m) {
  if (n == 0)
    return m;
  else
    return gcd(m % n, n);
}
long long k;
long long level = 0;
long long check1 = 0;
void check(long long k, long long p, long long mx, long long left) {
  if (p == k) {
    check1 = left;
    return;
  }
  level += 1;
  long long kk = 1LL << mx;
  if (k > p) {
    check(k, p + kk, mx - 1, 1);
  } else {
    check(k, p - kk, mx - 1, 0);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n, k;
  cin >> n >> k;
  long long mx = 0;
  for (long long i = 0; i <= 62; i++) {
    if (1LL << i & n) mx = max(mx, i);
  }
  long long p = 1LL << mx;
  long long p1 = mx + 1;
  for (long long i = 1; i <= k; i++) {
    cin >> n;
    level = 0;
    check(n, p, mx - 1, 0);
    long long lev = p1 - level - 1;
    string s;
    cin >> s;
    for (long long j = 0; j < s.size(); j++) {
      if (s[j] == 'L' || s[j] == 'R') {
        if (lev != 0) {
          if (s[j] == 'L') {
            long long p2 = 1LL << (lev - 1);
            check1 = 0;
            n -= p2;
            lev--;
          } else {
            long long p2 = 1LL << (lev - 1);
            check1 = 1;
            n += p2;
            lev--;
          }
        }
      } else {
        if (lev != mx) {
          long long p2 = 1LL << (lev);
          check(n, p, mx - 1, 0);
          if (check1 == 1)
            n -= p2;
          else
            n += p2;
          lev++;
        }
      }
    }
    cout << n << endl;
  }
}
