#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 15;
long long n, p, U, deplen[150], depbegin[150];
;
char s[maxn];
int dep;
int DFS(long long L, long long R, int dep) {
  long long mid = (L + R) >> 1;
  if (U == mid) return dep;
  if (U <= mid - 1)
    DFS(L, mid - 1, dep + 1);
  else
    DFS(mid + 1, R, dep + 1);
}
int main() {
  scanf("%lld%lld", &n, &p);
  long long t = n + 1;
  int DP;
  for (DP = 0; t / 2; DP++) depbegin[DP + 1] = t / 2, t /= 2;
  t = 2;
  for (int i = DP; i >= 1; i--) deplen[i] = t, t *= 2;
  while (p--) {
    scanf("%lld %s", &U, s + 1);
    dep = DFS(1, n, 1);
    int len = strlen(s + 1);
    for (int i = 1; i <= len; i++) {
      if (s[i] == 'U') {
        if (dep == 1) continue;
        long long A = U - depbegin[dep];
        A = A / deplen[dep];
        if (A % 2)
          U = (U * 2 - deplen[dep]) / 2;
        else
          U = (U * 2 + deplen[dep]) / 2;
        dep--;
      } else if (s[i] == 'L') {
        if (dep == DP) continue;
        dep++;
        U = U - deplen[dep] / 2;
      } else if (s[i] == 'R') {
        if (dep == DP) continue;
        dep++;
        U = U + deplen[dep] / 2;
      }
    }
    printf("%lld\n", U);
  }
  return 0;
}
