#include <bits/stdc++.h>
using namespace std;
int en;
long long n, q[100010];
char S[100010];
long long find_S(long long x, long long sum) {
  long long tmp = sum / 2LL;
  en = 0;
  q[++en] = tmp;
  while (tmp != x) {
    if (x < tmp)
      tmp = tmp - sum / 4LL;
    else
      tmp = tmp + sum / 4LL;
    sum /= 2LL;
    q[++en] = tmp;
  }
  return sum;
}
long long calc(long long x, char *c, long long sum) {
  while (*c != '\0') {
    if (*c == 'U') {
      if (en > 1) x = q[--en], sum *= 2LL;
    } else if (*c == 'L') {
      if (sum > 2) {
        x = x - sum / 4LL;
        q[++en] = x;
        sum /= 2LL;
      }
    } else {
      if (sum > 2) {
        x = x + sum / 4LL;
        q[++en] = x;
        sum /= 2LL;
      }
    }
    c++;
  }
  return x;
}
int main() {
  int q;
  long long x;
  cin >> n >> q;
  while (q--) {
    scanf("%I64d %s", &x, S);
    printf("%I64d\n", calc(x, S, find_S(x, n + 1LL)));
  }
  return 0;
}
