#include <bits/stdc++.h>
using namespace std;
long long n, q;
char s[100005];
int main() {
  long long cur, dep, maxDep, num, dif, flag;
  cin >> n >> q;
  maxDep = 0;
  num = 1;
  while (num < n + 1) {
    maxDep++;
    num *= 2;
  }
  while (q--) {
    cur = (n + 1) / 2;
    dep = 1;
    dif = (n + 1) / 2;
    long long u;
    cin >> u;
    while (cur != u) {
      if (u < cur) {
        dep++;
        cur -= dif / 2;
        dif /= 2;
      } else {
        dep++;
        cur += dif / 2;
        dif /= 2;
      }
    }
    scanf("%s", s);
    int len = strlen(s);
    for (int i = 0; i < len; i++) {
      if (s[i] == 'L') {
        if (dep == maxDep) continue;
        dep++;
        cur -= dif / 2;
        dif /= 2;
      } else if (s[i] == 'R') {
        if (dep == maxDep) continue;
        dep++;
        cur += dif / 2;
        dif /= 2;
      } else if (s[i] == 'U') {
        if (dep == 1) continue;
        dep--;
        if ((cur / (2 * dif)) % 2 == 1)
          cur -= dif;
        else
          cur += dif;
        dif *= 2;
      }
    }
    cout << cur << endl;
  }
  return 0;
}
