#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const double eps = 1e-9;
const int maxn = 1e5 + 9;
const double pi = 3.14159265358979323846264;
const long long inf = 2e16;
const long long mod = 1e9 + 7;
char op[maxn];
int num, le;
long long hj(int level, long long jk) {
  return (1ll << level) + (jk - 1) * (1ll << level + 1);
}
long long sol(int level, long long jk, long long m, int pos) {
  if (pos == num) return m;
  if (level == 0) {
    while (op[pos] == 'L' || op[pos] == 'R') ++pos;
    if (pos == num) return m;
  } else if (level == le) {
    while (op[pos] == 'U') ++pos;
    if (pos == num) return m;
  }
  if (op[pos] == 'U')
    return sol(level + 1, (jk + 1) / 2, hj(level + 1, (jk + 1) / 2), pos + 1);
  else if (op[pos] == 'L')
    return sol(level - 1, jk * 2 - 1, hj(level - 1, jk * 2 - 1), pos + 1);
  else
    return sol(level - 1, jk * 2, hj(level - 1, jk * 2), pos + 1);
}
int main() {
  ios::sync_with_stdio(false);
  long long n, q;
  cin >> n >> q;
  long long tmp = n + 1;
  le = 0;
  while (tmp > 1) {
    ++le;
    tmp /= 2;
  }
  --le;
  while (q--) {
    long long s;
    cin >> s >> op;
    if (n == 1) {
      cout << "1\n";
      continue;
    }
    num = strlen(op);
    int tmp = 0;
    long long jk = s;
    while (jk % 2 == 0) {
      jk >>= 1;
      ++tmp;
    }
    cout << sol(tmp, (jk + 1) / 2, s, 0) << "\n";
  }
  return 0;
}
