#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  long long int n, k, b;
  cin >> n >> k;
  long long int r;
  string p;
  bool left = false;
  bool right = false;
  long long int count = 0;
  for (long long j = (long long)(0); j <= (long long)(k - 1); j++) {
    left = false;
    right = false;
    cin >> r;
    b = r;
    count = 0;
    while (r % 2 == 0) {
      r = r / 2;
      count++;
    }
    if ((r - 1) % 4 == 0) {
      left = true;
    } else
      right = true;
    cin >> p;
    for (int i = 0; i < p.size(); i++) {
      if (p[i] == 'L') {
        if (count != 0)
          r = b - pow(2, count - 1);
        else
          r = b;
        if (count != 0) {
          count--;
          left = true;
          right = false;
        }
        b = r;
      } else if (p[i] == 'R') {
        if (count != 0)
          r = b + pow(2, count - 1);
        else
          r = b;
        if (count != 0) {
          count--;
          left = false;
          right = true;
        }
        b = r;
      } else {
        if (b != (n + 1) / 2) {
          if (left == true) {
            r = b + pow(2, count);
          } else if (right == true) {
            r = b - pow(2, count);
          }
        } else
          r = b;
        left = false;
        right = false;
        count = 0;
        b = r;
        while (r % 2 == 0) {
          count++;
          r = r / 2;
        }
        if ((r - 1) % 4 == 0)
          left = true;
        else
          right = true;
        r = b;
      }
    }
    cout << b << "\n";
  }
  return 0;
}
