#include <bits/stdc++.h>
using namespace std;
long long a[100], bin[100];
int main() {
  long long n, v, kq;
  int q, i, j, vt, num;
  string st;
  scanf("%lld %d", &n, &q);
  a[0] = 1;
  for (i = 1; i < 68; i++) {
    a[i] = a[i - 1] * 2;
    if (a[i] > n) {
      vt = i - 1;
      break;
    }
  }
  for (i = 0; i < q; i++) {
    scanf("%lld", &v);
    cin >> st;
    for (j = vt; j >= 0; j--) {
      bin[vt - j] = v / a[j];
      v %= a[j];
      if (bin[vt - j] == 1) num = vt - j;
    }
    for (j = 0; j < st.length(); j++) {
      if (st[j] == 'L' && num < vt) {
        bin[num] = 0;
        bin[num + 1] = 1;
        num++;
      }
      if (st[j] == 'R' && num < vt) {
        bin[num + 1] = 1;
        num++;
      }
      if (st[j] == 'U' && num > 0) {
        bin[num - 1] = 1;
        bin[num] = 0;
        num--;
      }
    }
    kq = 0;
    for (j = vt; j >= 0; j--) kq = kq + bin[vt - j] * a[j];
    printf("%lld\n", kq);
  }
  return 0;
}
