#include <bits/stdc++.h>
using namespace std;
class Solution {
  long long n;
  int k, q;
  long long root, key;
  vector<long long> u;
  vector<string> vs;
  long long des(long long u, string path) {
    for (int i = 0; i < path.size(); i++) {
      u = des(u, path[i]);
    }
    return u;
  }
  long long des(long long u, char move) {
    if (move == 'U') {
      long long m = 1;
      while ((u & m) == 0L) m = m << 1;
      if (m < root) {
        u -= m;
        m = m << 1;
        u = u | m;
      }
    } else if (move == 'L') {
      long long m = 1;
      while ((u & m) == 0L) m = m << 1;
      if (m > 1) u -= m / 2L;
    } else {
      long long m = 1;
      while ((u & m) == 0L) m = m << 1;
      if (m > 1) u += m / 2L;
    }
    return u;
  }

 public:
  void sol() {
    cin >> n >> q;
    k = 0;
    long long m = n + 1;
    root = m / 2L;
    while (m != 1) {
      k++;
      m = m >> 1;
    }
    u = vector<long long>(q);
    vs = vector<string>(q);
    for (int i = 0; i < q; i++) {
      cin >> u[i];
      cin >> vs[i];
    }
    for (int i = 0; i < q; i++) {
      cout << des(u[i], vs[i]) << '\n';
    }
  }
};
int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  Solution s;
  s.sol();
  return 0;
}
