*T* is a complete binary tree consisting of *n* vertices. It means that exactly one vertex is a root, and each vertex is either a leaf (and doesn't have children) or an inner node (and has exactly two children). All leaves of a complete binary tree have the same depth (distance from the root). So *n* is a number such that *n* + 1 is a power of 2.

In the picture you can see a complete binary tree with *n* = 15.

Vertices are numbered from 1 to *n* in a special recursive way: we recursively assign numbers to all vertices from the left subtree (if current vertex is not a leaf), then assign a number to the current vertex, and then recursively assign numbers to all vertices from the right subtree (if it exists). In the picture vertices are numbered exactly using this algorithm. It is clear that for each size of a complete binary tree exists exactly one way to give numbers to all vertices. This way of numbering is called symmetric.

You have to write a program that for given *n* answers *q* queries to the tree.

Each query consists of an integer number *u*<sub class="lower-index">*i*</sub> (1 ≤ *u*<sub class="lower-index">*i*</sub> ≤ *n*) and a string *s*<sub class="lower-index">*i*</sub>, where *u*<sub class="lower-index">*i*</sub> is the number of vertex, and *s*<sub class="lower-index">*i*</sub> represents the path starting from this vertex. String *s*<sub class="lower-index">*i*</sub> doesn't contain any characters other than 'L', 'R' and 'U', which mean traverse to the left child, to the right child and to the parent, respectively. Characters from *s*<sub class="lower-index">*i*</sub> have to be processed from left to right, considering that *u*<sub class="lower-index">*i*</sub> is the vertex where the path starts. If it's impossible to process a character (for example, to go to the left child of a leaf), then you have to skip it. The answer is the number of vertex where the path represented by *s*<sub class="lower-index">*i*</sub> ends.

For example, if *u*<sub class="lower-index">*i*</sub> = 4 and *s*<sub class="lower-index">*i*</sub> = «UURL», then the answer is 10.
The first line contains two integer numbers *n* and *q* (1 ≤ *n* ≤ 10<sup class="upper-index">18</sup>, *q* ≥ 1). *n* is such that *n* + 1 is a power of 2.

The next 2*q* lines represent queries; each query consists of two consecutive lines. The first of these two lines contains *u*<sub class="lower-index">*i*</sub> (1 ≤ *u*<sub class="lower-index">*i*</sub> ≤ *n*), the second contains non-empty string *s*<sub class="lower-index">*i*</sub>. *s*<sub class="lower-index">*i*</sub> doesn't contain any characters other than 'L', 'R' and 'U'.

It is guaranteed that the sum of lengths of *s*<sub class="lower-index">*i*</sub> (for each *i* such that 1 ≤ *i* ≤ *q*) doesn't exceed 10<sup class="upper-index">5</sup>.
none
15 2
4
UURL
8
LRLLLLLLLL
10
5
1 1
1
L
1
1 1
1
R
1
1 1
1
U
1
1 10
1
URLRLULUR
1
LRRRURULULL
1
LURURRUUUU
1
RRULLLRRUL
1
UULLUURL
1
RLRRULUL
1
LURRLRUULRR
1
LULLULUUUL
1
URULLULL
1
LRRLRUUUURRLRRL
1
1
1
1
1
1
1
1
1
1
3 10
2
RUUUULULULUU
1
ULLLURLU
3
LLURLULU
2
RRLURLURLLR
3
LRURURLRLLL
3
LRLULRRUURURRL
1
RULLR
2
LRULLURUL
3
RLL
1
ULRUULURLULLLLLLRLL
2
2
2
3
3
3
1
1
3
1
7 10
3
LLULULLLR
4
LUUURLLLUURRU
1
ULURR
6
LURLLLLRLR
2
ULULURU
7
RRUUUURLRLR
7
UUURRULRRLUL
7
ULLLRUULULR
6
UURRLL
6
RULUUULLRLLLUULL
1
6
3
7
4
5
5
3
7
1
15 10
1
URUUUR
15
RRLLURRRURL
1
UURRLUR
13
LRUULUURLRRRL
6
LULUURULUURL
15
UULL
8
ULLLULRLRUU
8
ULRUULRUURLLRL
5
LLUULRLURRRULLR
10
LUULLRU
12
15
7
11
10
9
4
9
1
10
31 10
12
RRRRRRULUURR
9
LUUURULLLLU
24
LLRRLURLLU
25
LLUUURL
14
RRRRRRULU
11
LRLUULRUULR
10
ULULU
30
LLRLLLLRULRLL
1
RRULRLRLLLRULR
20
ULLULLRR
15
2
18
26
14
11
12
29
1
17
4503599627370495 1
2251799813685248
LLLLLLLL
8796093022208
4503599627370495 1
2251799813685248
LLLLLL
35184372088832
576460752303423487 1
1125899906842624
R
1688849860263936
1125899906842623 1
1
UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUULLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRLULLLLLLLLLLLLLLLLUUUULRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
2147483648
n,q=map(int,input().split())
t=1
u=1
while t*2<n:
    t*=2
    u+=1
for h in range(q):
    a=int(input())
    s=input()
    b1=bin(a)
    b=[]

    for i in range(u-len(b1)+2):
        b.append("0")
    for i in range(2,len(b1)):
        b.append(b1[i])
    i=0
    #print(b)
    k = 0
    for j in range(len(b) - 1, 0, -1):
        if b[j] == "1":
            k = j
            #print(k)
            break
    while i<len(s):
        fl=0
        if s[i]=="R":
            while i<len(b) and k<len(b)-1 and s[i]=="R":
                b[k+1]="1"
                k+=1
                i+=1
                fl=1
        if i<len(s) and s[i]=="L":
            while i<len(b)and k<len(b)-1 and s[i]=="L":
                b[k]="0"
                b[k+1]="1"
                k+=1
                i+=1
                fl=1
        if i<len(s) and s[i]=="U":
            while k>0 and s[i]=="U":
                b[k]="0"
                b[k-1]="1"
                k-=1
                i+=1
                fl=1
        if not fl:i+=1
        #print(b,k)

    i=len(b)-1
    st=0
    k=0
    while i>=0:
        if b[i]=="1":
            k+=2**st
        st+=1
        i-=1
    print(k)