#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n;
  int q;
  cin >> n >> q;
  while (q--) {
    ll u;
    string s;
    cin >> u >> s;
    ll p = 1;
    while ((u % (p * 2)) == 0) p *= 2;
    for (char dir : s) {
      if (dir == 'U') {
        if (u != (n + 1) / 2) {
          if ((u + p) % (4 * p))
            u += p;
          else
            u -= p;
          p *= 2;
        }
      } else {
        if (p > 1) {
          p /= 2;
          if (dir == 'L')
            u -= p;
          else
            u += p;
        }
      }
    }
    cout << u << "\n";
  }
  return 0;
}
