#include <bits/stdc++.h>
using namespace std;
long long int n;
long long int f(long long int x) {
  long long int cnt = 1;
  while (x % 2 == 0) x /= 2, cnt *= 2;
  return cnt;
}
void solve() {
  long long int val;
  string s;
  cin >> val >> s;
  for (long long int i = 0; i < s.length(); i++) {
    long long int x = f(val);
    if (s[i] == 'L') val -= x / 2;
    if (s[i] == 'R') val += x / 2;
    long long int k = (val - x) / (2 * x);
    if (2 * val != n + 1) {
      if (s[i] == 'U' && k % 2) val -= x;
      if (s[i] == 'U' && k % 2 == 0) val += x;
    }
  }
  cout << val << endl;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  cin >> n;
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
