#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
template <typename T>
using v = vector<T>;
template <typename T>
using hset = unordered_set<T>;
template <typename T, typename U>
using hmap = unordered_map<T, U>;
const long long inf = 2e18;
ifstream fin("input.txt");
ofstream fout("output.txt");
void read() {}
template <typename Arg, typename... Args>
void read(Arg& arg, Args&... args) {
  cin >> (arg);
  read(args...);
}
void print() {}
template <typename Arg, typename... Args>
void print(Arg arg, Args... args) {
  cout << (arg) << " ";
  print(args...);
}
void debug() { cerr << endl; }
template <typename Arg, typename... Args>
void debug(Arg arg, Args... args) {
  cerr << (arg) << " ";
  debug(args...);
}
long long getInt() {
  long long a;
  cin >> a;
  return a;
}
void run() {
  long long n, q;
  read(n, q);
  for (; q; q--) {
    long long u;
    cin >> u;
    long long change = (n + 1) / 4;
    long long cur = (n + 1) / 2;
    stack<long long> pr;
    while (cur != u) {
      pr.push(cur);
      if (cur > u)
        cur -= change;
      else
        cur += change;
      change >>= 1;
    }
    string s;
    cin >> s;
    for (char i : s) {
      if (i == 'U' && !pr.empty()) {
        cur = pr.top();
        pr.pop();
        if (change)
          change <<= 1;
        else
          change++;
      } else if (i == 'L' && change) {
        pr.push(cur);
        cur -= change;
        change >>= 1;
      } else if (i == 'R' && change) {
        pr.push(cur);
        cur += change;
        change >>= 1;
      }
    }
    cout << cur;
    cout << endl;
  }
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  run();
  return 0;
}
