#include <bits/stdc++.h>
using namespace std;
void dout() { cerr << "\n"; }
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
  cerr << H << " ";
  dout(T...);
}
long long getSt(long long a) { return (a == 0 ? -1 : ((a ^ (a - 1)) + 1) / 2); }
long long go(long long n, long long cur, long long d) {
  long long st = getSt(cur);
  long long t = cur;
  t = cur;
  if (d == 0 && st > 1) t -= st / 2;
  if (d == 1 && st > 1) t += st / 2;
  long long t1, t2;
  if (d == 2) {
    t1 = cur + st;
    t2 = cur - st;
    if (getSt(t1) == st * 2)
      t = t1;
    else
      t = t2;
  }
  return (t < 1 || t > n ? cur : t);
}
signed main() {
  long long n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    long long cur;
    string s;
    cin >> cur >> s;
    for (auto ch : s) cur = go(n, cur, (ch == 'L' ? 0 : (ch == 'R' ? 1 : 2)));
    cout << cur << "\n";
  }
  return 0;
}
