#include <bits/stdc++.h>
using namespace std;
template <typename Tp>
inline void outarr(Tp _begin, Tp _end, const char* _delim = " ") {
  for (Tp current = _begin; current != _end; ++current) {
    std::cout << *current << _delim;
  }
  std::cout << '\n';
}
using ll = int64_t;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);
inline ll GoUp(ll v, ll n) {
  return v == (n >> 1) + 1 ? v
                           : (v & (v - 1)) | (1LL << (__builtin_ctzll(v) + 1));
}
inline ll GoLeft(ll v) {
  return v & 1 ? v : (v & (v - 1)) | (1LL << (__builtin_ctzll(v) - 1));
}
inline ll GoRight(ll v) {
  return v & 1 ? v : v | (1LL << (__builtin_ctzll(v) - 1));
}
inline ll Step(ll v, char cmd, int n) {
  switch (cmd) {
    case 'U':
      return GoUp(v, n);
    case 'L':
      return GoLeft(v);
    case 'R':
      return GoRight(v);
  }
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll n;
  int q;
  cin >> n >> q;
  for (int i = 0; i < (q); ++i) {
    ll v;
    string s;
    cin >> v >> s;
    for (char c : s) {
      v = Step(v, c, n);
    }
    cout << v << "\n";
  }
  return 0;
}
