#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 100010;
long long n, q, root, u, myAdd;
char tmp[N];
vector<long long> path;
void navigate(long long cur, long long toAdd, long long depth) {
  path.push_back(cur);
  if (cur == u) {
    myAdd = toAdd;
  }
  if (cur == u) return;
  if (u < cur) {
    navigate(cur + -toAdd, toAdd / 2, depth + 1);
  } else {
    navigate(cur + toAdd, toAdd / 2, depth + 1);
  }
}
bool isLeaf(long long cur) { return (cur & 1); }
int main() {
  scanf("%lld%lld", &n, &q);
  root = (n + 1) / 2;
  while (q--) {
    scanf("%lld", &u);
    scanf("%s", tmp);
    int len = strlen(tmp);
    path.clear();
    navigate(root, root / 2, 0);
    for (int i = 0; i < len; ++i) {
      if (isLeaf(path.back())) {
        if (tmp[i] == 'U' && path.size() > 1) {
          path.pop_back();
          myAdd++;
        }
      } else {
        if (tmp[i] == 'U') {
          if (path.size() > 1) {
            path.pop_back();
            myAdd *= 2;
          }
        } else {
          if (tmp[i] == 'L') {
            path.push_back(path.back() - myAdd);
          } else {
            path.push_back(path.back() + myAdd);
          }
          myAdd /= 2;
        }
      }
    }
    printf("%lld\n", path.back());
  }
  return 0;
}
