#include <bits/stdc++.h>
using namespace std;
long long po2[62];
long long n;
int maxlvl;
vector<int> v;
int cari(long long x) {
  long long tengah = (n + 1) / 2;
  int cur = maxlvl - 1;
  v.clear();
  while (tengah != x) {
    if (tengah > x)
      tengah -= po2[cur], v.push_back(0);
    else
      tengah += po2[cur], v.push_back(1);
    cur--;
  }
  return cur + 1;
}
int main(void) {
  std::ios::sync_with_stdio(false);
  int q;
  cin >> n >> q;
  po2[0] = 1;
  maxlvl = 0;
  for (int i = 1; i <= 60; i++) {
    po2[i] = po2[i - 1] * 2LL;
    if (po2[i] == (n + 1) / 2) maxlvl = i;
  }
  for (int i = 0; i < q; i++) {
    long long num;
    string str;
    cin >> num;
    cin >> str;
    int len = str.size();
    int pos = cari(num);
    for (int j = 0; j < len; j++) {
      assert(pos <= maxlvl && pos >= 0);
      if (str[j] == 'U') {
        if (pos == maxlvl) continue;
        if (v.back() == 0)
          num += po2[pos];
        else
          num -= po2[pos];
        v.pop_back();
        pos++;
      } else if (str[j] == 'L') {
        if (pos == 0) continue;
        pos--;
        num -= po2[pos];
        v.push_back(0);
      } else if (str[j] == 'R') {
        if (pos == 0) continue;
        pos--;
        num += po2[pos];
        v.push_back(1);
      }
    }
    cout << num << '\n';
  }
  return 0;
}
