#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int64_t n, q;
  cin >> n >> q;
  for (int i = 0; i < q; ++i) {
    int64_t u;
    string s;
    cin >> u >> s;
    int64_t level = 1;
    while (u % (level << 1) == 0) level <<= 1;
    for (char& move : s) {
      if (move == 'U') {
        if (level << 1 > n) continue;
        if ((u + level) % (level << 2) == 0)
          u -= level;
        else
          u += level;
        level <<= 1;
      } else {
        if (u & 1) continue;
        level >>= 1;
        if (move == 'L')
          u -= level;
        else
          u += level;
      }
    }
    cout << u << "\n";
  }
  return 0;
}
