#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
char dir[] = {'R', 'L', 'D', 'U'};
const int MOD = 1e9 + 7;
const long long INF = 1e9;
void setIO(string filename) {
  if (fopen((filename + ".in").c_str(), "r")) {
    freopen((filename + ".in").c_str(), "r", stdin);
    freopen((filename + ".out").c_str(), "w", stdout);
  }
}
long long n;
bitset<64> tobin(long long x) {
  bitset<64> b;
  long long currpow = 62;
  while (x > 0) {
    if ((1ll << currpow) <= x) {
      x -= (1ll << currpow);
      b[currpow] = 1;
    }
    currpow--;
  }
  return b;
}
long long unmask(bitset<64> b) {
  long long x = 0;
  for (long long i = 0; i < 64; i++)
    if (b[i]) x += (1ll << i);
  return x;
}
long long goup(long long x) {
  if (x == ((n + 1) / 2)) return x;
  bitset<64> b = tobin(x);
  int dep = 0;
  for (int i = 0; i < 64; i++)
    if (b[i]) {
      dep = i;
      break;
    }
  b[dep] = !b[dep];
  b[dep + 1] = 1;
  return unmask(b);
}
long long goleft(long long x) {
  if (x & 1) return x;
  bitset<64> b = tobin(x);
  int dep = 0;
  for (int i = 0; i < 64; i++)
    if (b[i]) {
      dep = i;
      break;
    }
  b[dep] = 0;
  b[dep - 1] = !b[dep - 1];
  return unmask(b);
}
long long goright(long long x) {
  if (x & 1) return x;
  bitset<64> b = tobin(x);
  int dep = 0;
  for (int i = 0; i < 64; i++)
    if (b[i]) {
      dep = i;
      break;
    }
  b[dep - 1] = !b[dep - 1];
  return unmask(b);
}
void solve() {
  int q;
  cin >> n >> q;
  while (q--) {
    long long x;
    string s;
    cin >> x >> s;
    for (char i : s) {
      if (i == 'L')
        x = goleft(x);
      else if (i == 'R')
        x = goright(x);
      else
        x = goup(x);
    }
    cout << x << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  for (int i = 1; i <= t; i++) solve();
  return 0;
}
