#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int _x;
  scanf("%d", &_x);
  return _x;
}
const int INF = (int)1e9;
const int mod = (int)1e9 + 7;
const int maxn = (int)1e5 + 5;
const int logn = 20;
const int sqrtn = 1e3 + 5;
long long int n, q, cur, depth, c_d, row, inf[100][2];
string path;
void up() {
  if (c_d == 1) return;
  row = (row + 1) / 2;
  c_d--;
  cur = inf[c_d][0] + inf[c_d][1] * (row - 1);
}
void left() {
  if (c_d == depth) return;
  row = (row - 1) * 2 + 1;
  c_d++;
  cur = inf[c_d][0] + inf[c_d][1] * (row - 1);
}
void right() {
  if (c_d == depth) return;
  row = (row - 1) * 2 + 2;
  c_d++;
  cur = inf[c_d][0] + inf[c_d][1] * (row - 1);
}
void find() {
  for (int i = 1; i <= depth; i++)
    if (cur % inf[i][1] == inf[i][0]) {
      c_d = i;
      row = (cur / inf[i][1]) + 1;
      break;
    }
}
int main() {
  long long int l, tmp;
  cin >> n >> q;
  depth = log2(n + 1);
  tmp = (1 << (depth - 1));
  for (long long int i = depth, t = 1; i >= 1; i--, t *= 2) {
    inf[i][0] = t;
    inf[i][1] = t * 2;
  }
  while (q--) {
    scanf("%lld", &cur);
    find();
    cin >> path;
    for (int i = 0; i < path.size(); i++) {
      if (path[i] == 'L') left();
      if (path[i] == 'R') right();
      if (path[i] == 'U') up();
    }
    printf("%lld\n", cur);
  }
  return 0;
}
