#include <bits/stdc++.h>
int main() {
  long long int n, a, k, a2, q;
  int i, j, b[64], d, tn, l;
  char s[100000];
  scanf("%lld %lld", &n, &q);
  a2 = n;
  for (tn = 0; a2 != 0; a2 = a2 / 2) tn++;
  for (i = 0; i != q; i++) {
    scanf("%lld", &a);
    scanf("%s", s);
    for (d = 0; d != tn; d++) {
      b[d] = a % 2;
      a = a / 2;
    }
    for (k = 0; b[k] == 0; k++) {
    }
    for (j = 0; s[j] != '\0'; j++) {
      if (s[j] == 'L' && b[0] != 1) {
        b[k] = 0;
        b[k - 1] = 1;
        k--;
      }
      if (s[j] == 'R' && b[0] != 1) {
        b[k - 1] = 1;
        k--;
      }
      if (s[j] == 'U' && k != tn - 1) {
        b[k + 1] = 1;
        b[k] = 0;
        k++;
      }
    }
    a = 0;
    for (j = tn - 1; j != -1; j--) a = 2 * a + b[j];
    printf("%lld\n", a);
  }
  return 0;
}
