#include <bits/stdc++.h>
using namespace std;
void scan(vector<int>& v) {
  for (int i = 0; i < v.size(); i++) scanf("%d", &v[i]);
}
void print(vector<int>& v) {
  for (int i = 0; i < v.size(); i++) printf("%d ", v[i]);
}
long long n, q;
long long fin(long long total, long long par, long long node, long long ch,
              long long level) {
  if (total == node) {
    if (ch == 'U') return par;
    if (ch == 'L') {
      return total - (level);
    }
    if (ch == 'R') {
      return total + level;
    }
  }
  if (node < total) {
    return fin(total - level, total, node, ch, level / 2);
  } else {
    return fin(total + level, total, node, ch, level / 2);
  }
}
void fun() {
  cin >> n >> q;
  while (q--) {
    long long node;
    cin >> node;
    string ss;
    cin >> ss;
    for (long long i = 0; i < ss.length(); i++) {
      if ((ss[i] == 'L' || ss[i] == 'R') && (node % 2 == 1)) continue;
      long long n2 = fin((n + 1) / 2, -1, node, ss[i], (n + 1) / 4);
      if (n2 != -1) node = n2;
    }
    cout << node << endl;
  }
}
int main() {
  bool test = 0;
  if (test == 1) {
    freopen("input_file_name.txt", "r", stdin);
    freopen("output_file_name.txt", "w", stdout);
  }
  int t = 1;
  for (int i = 1; i <= t; i++) {
    fun();
  }
  int acc;
  cin >> acc;
  return 0;
}
