#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " : " << a << endl;
  err(++it, args...);
}
vector<pair<long long int, long long int> > v, dd;
long long int n, q;
bool ch(long long int lvl, long long int foo) {
  long long int op = (foo - v[lvl].first) / dd[lvl].first;
  bool chi;
  if (op % 2 == 0)
    chi = false;
  else
    chi = true;
  return chi;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  ;
  cin >> n >> q;
  v.push_back({0, 0});
  dd.push_back({0, 0});
  long long int l = 1, r = n, jump = 1, layers = 0;
  while (l <= r) {
    layers++;
    v.push_back({l, r});
    l += jump, r -= jump;
    jump *= 2;
    dd.push_back({jump, jump / 2});
  }
  while (q--) {
    long long int foo;
    cin >> foo;
    string s;
    cin >> s;
    long long int lvl;
    bool chi;
    for (int i = 1; i < (dd).size(); i++) {
      if (foo % dd[i].first == dd[i].second) {
        lvl = i;
        break;
      }
    }
    chi = ch(lvl, foo);
    long long int c_lvl = lvl;
    for (long long int i = (long long int)(0); i < (long long int)((s).size());
         i++) {
      if ((c_lvl == 1 and (s[i] == 'L' or s[i] == 'R')) or
          (c_lvl == layers and s[i] == 'U'))
        continue;
      else {
        if (s[i] == 'U') {
          foo = ((chi) ? (foo - dd[c_lvl].second) : (foo + dd[c_lvl].second));
          c_lvl++;
          chi = ch(c_lvl, foo);
        } else if (s[i] == 'L') {
          foo = foo - dd[c_lvl].second / 2;
          c_lvl--;
          chi = ch(c_lvl, foo);
        } else {
          foo = foo + dd[c_lvl].second / 2;
          c_lvl--;
          chi = ch(c_lvl, foo);
        }
      }
    }
    cout << foo << endl;
  }
  return 0;
}
