#include <bits/stdc++.h>
using namespace std;
int Dx[] = {1, -1, 0, 0};
int Dy[] = {0, 0, 1, -1};
void fast() {
  ios::sync_with_stdio(0);
  cin.tie(0);
}
int main() {
  fast();
  long long n, q;
  cin >> n >> q;
  while (q--) {
    long long a = (n + 1) / 2, p = (n + 1) / 2;
    map<long long, long long> par;
    long long z;
    string s;
    cin >> z >> s;
    while (a != z) {
      par[a + p / 2] = par[a - p / 2] = a;
      if (z > a)
        a += p / 2;
      else
        a -= p / 2;
      p /= 2;
    }
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == 'L') {
        if (a % 2) continue;
        par[a - p / 2] = par[a + p / 2] = a;
        a -= p / 2;
        p /= 2;
      } else if (s[i] == 'R') {
        if (a % 2) continue;
        par[a - p / 2] = par[a + p / 2] = a;
        a += p / 2;
        p /= 2;
      } else {
        if (a == (n + 1) / 2) continue;
        a = par[a];
        p *= 2;
      }
    }
    cout << a << endl;
  }
  return 0;
}
