#include <bits/stdc++.h>
using namespace std;
long long h(long long v) {
  for (int i = 63 - 1; i >= 1; i--) {
    if ((v - (1ll << (long long)(i - 1))) % (1ll << (long long)i) == 0) {
      return (long long)i;
    }
  }
}
class Solution {
 public:
  void solve(std::istream& in, std::ostream& out) {
    long long n, q;
    in >> n >> q;
    long long v;
    string path;
    long long maxH = (n + 1) / 2ll;
    while (q--) {
      in >> v >> path;
      long long H = h(v);
      for (int i = 0; i < path.size(); i++) {
        char c = path[i];
        if (c == 'U') {
          if (v == maxH) continue;
          long long foo = 1ll << (H - 1);
          long long V = v - foo;
          if ((V / (foo * 2)) % 2) {
            v -= foo;
          } else
            v += foo;
          H++;
        }
        if (c == 'R') {
          if (v % 2 == 1) continue;
          long long foo = 1ll << (H - 1);
          v += foo / 2;
          H--;
        }
        if (c == 'L') {
          if (v % 2 == 1) continue;
          long long foo = 1ll << (H - 1);
          v -= foo / 2;
          H--;
        }
      }
      out << v << endl;
    }
  }
};
void solve(std::istream& in, std::ostream& out) {
  out << std::setprecision(15);
  int T = 1;
  if (0) in >> T;
  Solution solution;
  if (true)
    while (T--) solution.solve(in, out);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  istream& in = cin;
  ostream& out = cout;
  solve(in, out);
  return 0;
}
