#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================
vi g[N];
int a[N], e[N];
string s;
ll root ;
#define L 0
#define R 1
struct node{
ll val;
int lev;
int dir;
ll dif;
void out(){
cout << val <<" " << lev << " " <<dir << " " << dif << endl;
}
void par(){
if(val==root)return; //invalid move
if(dir==R)
val-=dif;
else val+=dif;
copy(val);
}
void left(){
if(dif==1)return; //invalid move
dif/=2;
val-=dif;
copy(val);
}
void right(){
if(dif==1)return; //invalid move
dif/=2;
val+=dif;
//found new val, update current node
copy(val);
}
void copy(ll x){
node to = go(x);
val = to.val;
lev = to.lev;
dif = to.dif;
dir = to.dir;
}
//takes a number x, and returns the particular
//details of x = {value, level, left/right, difference with parent}
node go(ll x){
ll dif = root/2;
ll cur = root;
int l = 0;
int dir = -1;
while(cur != x){
l++;
if(cur<x){
cur+=dif;
dir=R;
}
else cur-=dif, dir=L;
dif /= 2;
}
node ans = {x,l,dir,max(1LL,dif*2)};
return ans;
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll i,n,k,j,q,u;
cin >> n >> q;
root = (n+1)/2;
while(q--){
cin >> u;
cin >> s;
n = s.size();
node cur = cur.go(u);
fo(i, n){
if(s[i]=='U') cur.par();
else if(s[i]=='L') cur.left();
else if(s[i]=='R') cur.right();
}
cout << cur.val << endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgZ2MgZ2V0Y2hhcl91bmxvY2tlZAojZGVmaW5lIGZvKGksbikgZm9yKGk9MDtpPG47aSsrKQojZGVmaW5lIEZvKGksayxuKSBmb3IoaT1rO2s8bj9pPG46aT5uO2s8bj9pKz0xOmktPTEpCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgc2koeCkJc2NhbmYoIiVkIiwmeCkKI2RlZmluZSBzbCh4KQlzY2FuZigiJWxsZCIsJngpCiNkZWZpbmUgc3MocykJc2NhbmYoIiVzIixzKQojZGVmaW5lIHBpKHgpCXByaW50ZigiJWRcbiIseCkKI2RlZmluZSBwbCh4KQlwcmludGYoIiVsbGRcbiIseCkKI2RlZmluZSBwcyhzKQlwcmludGYoIiVzXG4iLHMpCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiNkZWZpbmUgRiBmaXJzdAojZGVmaW5lIFMgc2Vjb25kCiNkZWZpbmUgYWxsKHgpIHguYmVnaW4oKSwgeC5lbmQoKQojZGVmaW5lIGNscih4KSBtZW1zZXQoeCwgMCwgc2l6ZW9mKHgpKQojZGVmaW5lIHNvcnRhbGwoeCkgc29ydChhbGwoeCkpCiNkZWZpbmUgdHIoaXQsIGEpIGZvcihhdXRvIGl0ID0gYS5iZWdpbigpOyBpdCAhPSBhLmVuZCgpOyBpdCsrKQojZGVmaW5lIFBJIDMuMTQxNTkyNjUzNTg5NzkzMjM4NDYyNgp0eXBlZGVmIHBhaXI8aW50LCBpbnQ+CXBpaTsKdHlwZ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