#include <bits/stdc++.h>
using namespace std;
long long int pow2[63];
long long int n;
int findLevelOfRoot(long long int root) {
  int current = 63;
  while (1) {
    if (root & pow2[63 - current]) return 63 - current;
    current--;
  }
}
long long int calculate(long long int root, string mvmt) {
  int currentLevel = findLevelOfRoot(root);
  long long int currentNumber = root;
  for (int i = 0; i < mvmt.size(); i++) {
    if (mvmt[i] == 'L' || mvmt[i] == 'R') {
      if (currentLevel == 0)
        continue;
      else {
        currentLevel--;
        if (mvmt[i] == 'L')
          currentNumber -= pow2[currentLevel];
        else
          currentNumber += pow2[currentLevel];
      }
    } else {
      if (currentNumber == (n + 1) / 2) continue;
      if (currentNumber & pow2[currentLevel + 1])
        currentNumber -= pow2[currentLevel];
      else
        currentNumber += pow2[currentLevel];
      currentLevel++;
    }
  }
  return currentNumber;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> n >> q;
  pow2[0] = 1;
  for (int i = 1; i < 63; i++) pow2[i] = (pow2[i - 1] << 1);
  for (int i = 0; i < q; i++) {
    long long int root;
    cin >> root;
    string mvmt;
    cin >> mvmt;
    cout << calculate(root, mvmt) << endl;
  }
}
