#include <bits/stdc++.h>
using namespace std;
long long n, u, d, node, q, x;
stack<int> st;
string s;
int main() {
  cin >> n >> q;
  while (q--) {
    scanf("%lld", &x);
    cin >> s;
    node = (n + 1) / 2;
    u = 0;
    d = node / 2;
    while (node != x) {
      if (node > x) {
        st.push(1);
        node -= d;
        u = d;
        d /= 2;
      } else {
        st.push(0);
        node += d;
        u = d;
        d /= 2;
      }
    }
    for (int i = 0; i < s.size(); ++i) {
      if (s[i] == 'L') {
        if (node % 2 == 0) {
          st.push(1);
          node -= d;
          if (d != 0) {
            u = d;
          }
          d /= 2;
        }
      } else if (s[i] == 'R') {
        if (node % 2 == 0) {
          st.push(0);
          node += d;
          if (d != 0) {
            u = d;
          }
          d /= 2;
        }
      } else {
        if (node == (n + 1) / 2) {
          u = 0;
        } else {
          if (st.top() == 1) {
            node += u;
          } else {
            node -= u;
          }
          st.pop();
          d = u;
          u *= 2;
        }
      }
    }
    printf("%lld\n", node);
  }
  return 0;
}
