#include <bits/stdc++.h>
using namespace std;
const long long MAXlog = 100;
long long tavan_2[MAXlog];
long long n, q, logn;
long long give_log() {
  long long num = n;
  long long cnt = 0;
  while (num) {
    num /= 2;
    cnt++;
  }
  return cnt;
}
long long give_h(long long num) {
  long long cnt = 0;
  while (num % 2 == 0) {
    cnt++;
    num /= 2;
  }
  return logn - cnt;
}
long long give_simp(long long num, long long h) {
  return (num + tavan_2[logn - h]) / tavan_2[logn - h + 1];
}
long long give_comp(long long num, long long h) {
  return (num * tavan_2[logn - h + 1]) - tavan_2[logn - h];
}
int32_t main() {
  cin >> n >> q;
  logn = give_log();
  tavan_2[0] = 1;
  for (long long i = 1; i <= logn; i++) tavan_2[i] = tavan_2[i - 1] * 2;
  for (long long t = 0; t < q; t++) {
    long long v;
    string mve;
    cin >> v >> mve;
    long long h = give_h(v);
    v = give_simp(v, h);
    for (long long i = 0; i < mve.size(); i++) {
      if (mve[i] == 'U' && h > 1) {
        v = (v + 1) / 2;
        h--;
      }
      if (mve[i] == 'R' && h < logn) {
        v = v * 2;
        h++;
      } else if (mve[i] == 'L' && h < logn) {
        v = v * 2 - 1;
        h++;
      }
    }
    cout << give_comp(v, h) << ' ';
  }
  cout << endl;
}
