#include <bits/stdc++.h>
using namespace std;
long long int n, q, num;
long long int ans = 0;
string str;
int main() {
  cin >> n >> q;
  while (q--) {
    cin >> num;
    cin >> str;
    ans = num;
    for (int i = 0; i < str.length(); i++) {
      char ch = str[i];
      long long int level = 1;
      while (num % (2 * level) == 0) {
        level = 2 * level;
      }
      if (ch == 'U') {
        if (num != (n + 1) / 2) {
          if (((num - 1) / level) % 4 == 0) {
            num += level;
          } else {
            num -= level;
          }
        }
      } else if (ch != 'U') {
        if (num % 2 == 0) {
          if (ch == 'L') {
            num = num - level / 2;
          } else {
            num = num + level / 2;
          }
        }
      }
    }
    cout << num << endl;
  }
  return 0;
}
