#include <bits/stdc++.h>
char s[100001];
int q, len;
long long n, u, level;
int main() {
  scanf("%I64d %d", &n, &q);
  for (int i = 0; i < q; i++) {
    scanf("%I64d", &u);
    scanf("%s", s);
    len = strlen(s);
    level = 1;
    while (u % (level * 2) == 0) level *= 2;
    for (int j = 0; j < len; j++) {
      if (s[j] == 'L' && level > 1) {
        level /= 2;
        u -= level;
      } else if (s[j] == 'R' && level > 1) {
        level /= 2;
        u += level;
      } else if (s[j] == 'U' && level <= n / 2) {
        u += level;
        level *= 2;
        if ((u / level) % 2 == 0) u -= level;
      }
    }
    printf("%I64d\n", u);
  }
  return 0;
}
