#include <bits/stdc++.h>
using namespace std;
long long int sett(long long int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
void update(long long &x, long long val) {
  x = x + val;
  if (x > 1000000007) x -= 1000000007;
}
void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL), cout.tie(NULL);
}
int moves[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
long long int pow(int k) {
  long long int ans = 1;
  while (k--) {
    ans *= 10;
  }
  return ans;
}
string num_to_string(long long int num) {
  long long int bal;
  bal = num;
  string Result;
  stringstream convert;
  convert << bal;
  Result = convert.str();
  return Result;
}
long long int string_to_num(string Text) {
  long long int Result;
  stringstream convert(Text);
  if (!(convert >> Result)) Result = 0;
  return Result;
}
bool cmp(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) {
  if (a.first < b.first)
    return 1;
  else
    return 0;
}
int find_level(long long int a) {
  int ans = 0;
  while (a) {
    ans++;
    if (a & 1) break;
    a = a >> 1;
  }
  return ans;
}
int find_position(long long int a) {
  while (a) {
    if (a & 1) {
      a = a >> 1;
      break;
    }
    a = a >> 1;
  }
  if (a % 2 == 0)
    return 1;
  else
    return 0;
}
long long int bal[100];
int main() {
  bal[0] = 1;
  for (int i = 1; i < 70; i++) {
    long long int p = 2;
    bal[i] = bal[i - 1] * 2;
  }
  long long int n, k;
  scanf("%lld%lld", &n, &k);
  long long int sal = n + 1;
  int level = 0;
  while (sal) {
    level++;
    sal /= 2;
  }
  level--;
  char a[100100];
  while (k--) {
    long long int start;
    scanf("%I64d", &start);
    scanf("%s", a);
    int len = strlen(a);
    long long int cur = start;
    int lvl = find_level(cur);
    for (int i = 0; i < len; i++) {
      if (a[i] == 'U') {
        if (lvl == level) {
        } else {
          int which_child = find_position(cur);
          if (which_child == 0) {
            cur = cur - bal[lvl - 1];
          } else {
            cur = cur + bal[lvl - 1];
          }
          lvl++;
        }
      } else {
        if (lvl == 1) {
        } else {
          if (a[i] == 'R') {
            cur = cur + bal[lvl - 2];
          } else {
            cur = cur - bal[lvl - 2];
          }
          lvl--;
        }
      }
    }
    printf("%I64d\n", cur);
  }
  return 0;
}
