#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  long long n, q, md;
  cin >> n >> q;
  md = (n + 1) / 2;
  for (long long i = 0; i < q; i++) {
    long long k;
    string s;
    cin >> k >> s;
    stack<long long> S;
    long long d = md, p = md;
    while (p != k) {
      d = d / 2;
      if (p > k) {
        p = p - d;
        S.push(-d);
      } else {
        p = p + d;
        S.push(d);
      }
    }
    for (long long j = 0; j < s.size(); j++) {
      if (s[j] == 'L') {
        if (d == 1) continue;
        d = d / 2;
        p = p - d;
        S.push(-d);
      }
      if (s[j] == 'R') {
        if (d == 1) continue;
        d = d / 2;
        p = p + d;
        S.push(d);
      }
      if (s[j] == 'U') {
        if (d == md) continue;
        d = d * 2;
        p = p - S.top();
        S.pop();
      }
    }
    cout << p << "\n";
  }
}
