#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
template <typename T>
T gcd(T a, T b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long power(long long a, long long b, long long m = 1000000007) {
  long long answer = 1;
  while (b) {
    if (b & 1) answer = (answer * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return answer;
}
using namespace std;
void ctrl() { cout << "Control\n"; }
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long t;
  cin >> t;
  while (t--) {
    long long l;
    cin >> l;
    string p;
    cin >> p;
    for (long long i = 0; i < p.length(); i++) {
      if (p[i] == 'L') {
        long long temp = (l & (-l)) / 2;
        if (temp < l) l -= temp;
      } else if (p[i] == 'R') {
        long long temp = (l & (-l)) / 2;
        if (temp + l <= n) l += temp;
      } else {
        long long temp = (l & (-l));
        temp = temp * 2;
        if ((temp & l)) {
          if (l - (temp / 2) > 0) l -= (temp / 2);
        } else {
          if (l + (temp / 2) <= n && __builtin_popcount(l) != 1)
            l += (temp / 2);
          else if (__builtin_popcount(l) == 1 && l + l <= n)
            l += l;
        }
      }
    }
    cout << l << endl;
  }
  return 0;
}
