#include <bits/stdc++.h>
using namespace std;
char str[100100];
int main() {
  long long n;
  int q;
  scanf("%I64d%d", &n, &q);
  int k = 0;
  long long tmp = n + 1;
  while (tmp % 2 == 0) tmp /= 2, k++;
  while (q--) {
    long long st;
    scanf("%I64d", &st);
    cin >> str;
    int L = strlen(str);
    for (int i = 0; i < L; i++) {
      int t = 0;
      while (st % 2 == 0) t++, st /= 2;
      if (str[i] == 'U') {
        if (t == k - 1) {
          st = (1ll << t);
          continue;
        }
        if (st % 4 == 3)
          st = (st - 1) * (1ll << t);
        else
          st = (st + 1) * (1ll << t);
      } else {
        if (t == 0) continue;
        t--;
        st *= 2;
        if (str[i] == 'L')
          st = (st - 1) * (1ll << t);
        else
          st = (st + 1) * (1ll << t);
      }
    }
    printf("%I64d\n", st);
  }
  return 0;
}
