#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c *x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug &operator<<(const c &) {
    return *this;
  }
};
long long n;
vector<long long> ups;
long long getRoot() { return n / 2; }
long long getUp(long long cur, int &depth) {
  if (cur == n / 2)
    return cur;
  else {
    depth--;
    long long ans = ups[ups.size() - 1];
    ups.pop_back();
    return ans;
  }
}
long long getLeft(long long cur, int &depth) {
  if (cur % 2 == 1)
    return cur;
  else {
    ups.push_back(cur);
    long long ans = cur - (cur & (~(cur - 1))) / 2;
    debug() << " ["
            << "depth"
               ": "
            << (depth)
            << "] "
               " ["
            << "cur"
               ": "
            << (cur)
            << "] "
               " ["
            << "ans"
               ": "
            << (ans) << "] ";
    depth++;
    if (ans <= 0) ans = 1;
    return ans;
  }
}
long long getRight(long long cur, int &depth) {
  if (cur % 2 == 1)
    return cur;
  else {
    ups.push_back(cur);
    long long ans = cur + (cur & (~(cur - 1))) / 2;
    depth++;
    if (ans >= n) ans = n - 1;
    return ans;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  cin >> n;
  n = n + 1;
  long long root = getRoot();
  int q;
  cin >> q;
  while (q--) {
    long long p;
    cin >> p;
    string s;
    cin >> s;
    long long cur = root;
    int depth = 1;
    ups.clear();
    while (cur != p) {
      debug() << " ["
              << "cur"
                 ": "
              << (cur) << "] ";
      if (p < cur) {
        cur = getLeft(cur, depth);
      } else {
        cur = getRight(cur, depth);
      }
    }
    for (int i = 0; i < s.length(); i++) {
      debug() << " ["
              << "cur"
                 ": "
              << (cur)
              << "] "
                 " ["
              << "depth"
                 ": "
              << (depth) << "] ";
      switch (s[i]) {
        case 'U':
          cur = getUp(cur, depth);
          break;
        case 'L':
          cur = getLeft(cur, depth);
          break;
        case 'R':
          cur = getRight(cur, depth);
          break;
      }
    }
    cout << cur << endl;
  }
  return 0;
}
