#include <bits/stdc++.h>
using namespace std;
long long id;
long long kth(long long p, long long sum, long long k) {
  long long half = (sum - 1) / 2;
  if (k <= half) return kth(p * 2, half, k);
  if (half + 1 == k) {
    id = p;
    return sum;
  }
  return kth(p * 2 + 1, half, k - half - 1);
}
int main() {
  long long n, q;
  cin >> n >> q;
  for (int i = 0; i < q; ++i) {
    long long k, t, half;
    string s;
    cin >> k >> s;
    long long sum = kth(1, n, k);
    for (int j = 0; j < s.size(); ++j) {
      char c = s[j];
      half = sum / 2;
      if (c == 'U') {
        if (id == 1) continue;
        if (id % 2 == 0) {
          t = k + half + 1;
        } else {
          t = k - half - 1;
        }
        k = t;
        sum = sum * 2 + 1;
        id /= 2;
      }
      if (c == 'L') {
        if (sum == 1) continue;
        t = k - half / 2 - 1;
        k = t;
        sum = half;
        id = id * 2;
      }
      if (c == 'R') {
        if (sum == 1) continue;
        t = k + half / 2 + 1;
        k = t;
        sum = half;
        id = id * 2 + 1;
      }
    }
    cout << k << endl;
  }
  return 0;
}
