#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e6 + 100;
const long long INF = 9 * (long long)(1e18);
const long double PI = 3.14159265358979323846;
long long int modexp(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  if (y == 0)
    return 1;
  else if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res % p;
}
vector<long long int> is_prime(N + 1, 1);
void sieve(long long int n) {
  for (long long int i = 1; i <= n; i++) is_prime[i] = i;
  for (long long int i = 2; i * i <= n; i++) {
    if (is_prime[i] == i) {
      for (long long int j = i * i; j <= n; j += i)
        is_prime[j] = min(i, is_prime[j]);
    }
  }
}
long long int level(long long int temp) {
  long long int lvl = 0;
  while (temp % 2 == 0) {
    temp /= 2;
    lvl++;
  }
  return lvl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, q;
  cin >> n >> q;
  while (q--) {
    long long int u;
    cin >> u;
    string s;
    cin >> s;
    long long int lvl;
    long long int top = log2((n + 1) / 2);
    long long int len = s.length();
    lvl = level(u);
    for (long long int i = 0; i <= len - 1; i++) {
      if (lvl == 0 && (s[i] == 'L' || s[i] == 'R'))
        continue;
      else if (lvl == top && s[i] == 'U')
        continue;
      else {
        if (s[i] == 'L')
          u = u - (1ll << (lvl - 1));
        else if (s[i] == 'R')
          u = u + (1ll << (lvl - 1));
        else {
          if (level(u + (1ll << lvl)) == lvl + 1)
            u = u + (1ll << lvl);
          else
            u = u - (1ll << lvl);
        }
        if (s[i] == 'L' || s[i] == 'R')
          lvl--;
        else
          lvl++;
      }
    }
    cout << u << "\n";
  }
}
