#include <bits/stdc++.h>
using namespace std;
long long btcnt(long long n) {
  long long k = 0;
  while (n % 2 == 0) {
    k++;
    n /= 2;
  }
  return k;
}
long long arr[100];
int main() {
  long long n, q;
  cin >> n >> q;
  long long mx = btcnt(n + 1) - 1;
  arr[0] = 1;
  for (long long i = 1; i <= 64; i++) {
    arr[i] = arr[i - 1] * 2;
  }
  while (q--) {
    long long pvt;
    cin >> pvt;
    string s;
    cin >> s;
    for (int i = 0; i < s.size(); i++) {
      long long temp = btcnt(pvt);
      if (s[i] == 'U') {
        if (temp == mx) continue;
        long long a, b;
        a = pvt + arr[temp];
        b = pvt - arr[temp];
        if (a % arr[temp + 1] == 0 && a % arr[temp + 2] != 0)
          pvt = a;
        else
          pvt = b;
      } else if (s[i] == 'L') {
        if (temp == 0)
          continue;
        else {
          pvt -= arr[temp - 1];
        }
      } else {
        if (temp == 0)
          continue;
        else {
          pvt += arr[temp - 1];
        }
      }
    }
    cout << pvt << endl;
  }
  return 0;
}
