#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, q;
  long long i, num;
  char s[100000 + 10];
  cin >> n >> q;
  long long lay = (n + 1) / 2;
  while (q--) {
    cin >> num;
    cin >> s;
    int len = strlen(s);
    for (i = 0; i < len; i++) {
      if (num == lay && s[i] == 'U') {
        continue;
      } else if (num % 2 == 1 && (s[i] == 'L' || s[i] == 'R'))
        continue;
      else {
        long long pos = num & (-num);
        if (s[i] == 'U') {
          if (((num - pos) / (pos * 2)) % 2 == 0)
            num += pos;
          else if (((num - pos) / (pos * 2)) % 2 == 1) {
            num -= pos;
          }
        } else if (s[i] == 'L') {
          num -= pos / 2;
        } else if (s[i] == 'R') {
          num += pos / 2;
        }
      }
    }
    cout << num << endl;
  }
  return 0;
}
