#include <bits/stdc++.h>
using namespace std;
inline unsigned long long sL(unsigned long long a) {
  a = a & -a;
  a /= 2;
  return a;
}
inline unsigned long long L(unsigned long long a) { return a & -a; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  unsigned long long n, q;
  cin >> n >> q;
  unsigned long long now;
  string steps;
  for (unsigned long long i = 0; i < q; i++) {
    cin >> now;
    cin >> steps;
    for (int j = 0; j < steps.size(); j++) {
      if (steps[j] == 'L') {
        if (now - sL(now) > 0) now -= sL(now);
      }
      if (steps[j] == 'R') {
        if (now + sL(now) <= n) now += sL(now);
      }
      if (steps[j] == 'U') {
        unsigned long long temp = now + L(now);
        if (temp - sL(temp) == now && temp <= n)
          now = temp;
        else if (now - L(now) > 0)
          now -= L(now);
      }
    }
    cout << now << endl;
  }
  return 0;
}
