You have got a shelf and want to put some books on it.

You are given $q$ queries of three types:  L $id$ — put a book having index $id$ on the shelf to the left from the leftmost existing book;  R $id$ — put a book having index $id$ on the shelf to the right from the rightmost existing book;  ? $id$ — calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index $id$ will be leftmost or rightmost. 

You can assume that the first book you will put can have any position (it does not matter) and queries of type $3$ are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don't put the same book on the shelf twice, so $id$s don't repeat in queries of first two types.

Your problem is to answer all the queries of type $3$ in order they appear in the input.

Note that after answering the query of type $3$ all the books remain on the shelf and the relative order of books does not change.

If you are Python programmer, consider using PyPy instead of Python when you submit your code.


-----Input-----

The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries.

Then $q$ lines follow. The $i$-th line contains the $i$-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type $3$, it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).

It is guaranteed that there is at least one query of type $3$ in the input.

In each query the constraint $1 \le id \le 2 \cdot 10^5$ is met.


-----Output-----

Print answers to queries of the type $3$ in order they appear in the input.


-----Examples-----
Input
8
L 1
R 2
R 3
? 2
L 4
? 1
L 5
? 1

Output
1
1
2

Input
10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115

Output
0
2
1



-----Note-----

Let's take a look at the first example and let's consider queries:   The shelf will look like $[1]$;  The shelf will look like $[1, 2]$;  The shelf will look like $[1, 2, 3]$;  The shelf looks like $[1, \textbf{2}, 3]$ so the answer is $1$;  The shelf will look like $[4, 1, 2, 3]$;  The shelf looks like $[4, \textbf{1}, 2, 3]$ so the answer is $1$;  The shelf will look like $[5, 4, 1, 2, 3]$;  The shelf looks like $[5, 4, \textbf{1}, 2, 3]$ so the answer is $2$. 

Let's take a look at the second example and let's consider queries:   The shelf will look like $[100]$;  The shelf will look like $[100, 100000]$;  The shelf will look like $[100, 100000, 123]$;  The shelf will look like $[101, 100, 100000, 123]$;  The shelf looks like $[101, 100, 100000, \textbf{123}]$ so the answer is $0$;  The shelf will look like $[10, 101, 100, 100000, 123]$;  The shelf will look like $[10, 101, 100, 100000, 123, 115]$;  The shelf looks like $[10, 101, \textbf{100}, 100000, 123, 115]$ so the answer is $2$;  The shelf will look like $[10, 101, 100, 100000, 123, 115, 110]$;  The shelf looks like $[10, 101, 100, 100000, 123, \textbf{115}, 110]$ so the answer is $1$.

Difficulty: introductory

Starter code:

Solutions:
n = int(input())
d = {}
matr = [0] * (2 * n + 1)
head = n - 1
tail = n
for i in range(n):
	st, n = input().split()
	n = int(n)
	if st == 'L':
		matr[head] = n
		d[n] = head
		head -= 1
	elif st == 'R':
		matr[tail] = n
		d[n] = tail
		tail += 1
	else:
		print(min(d[n] - head, tail - d[n]) - 1)


n=int(input())
a=[0]*200001
mi=0
ma=0
input()
for i in range(n-1):
    r,id=input().split()
    id=int(id)
    if r=='L':
        mi-=1
        a[id]=mi
    elif r=='R':
        ma+=1
        a[id]=ma
    else:
        print(min(a[id]-mi,ma-a[id]))

q = int(input())

if q > 0:
	left = 0
	right = 0

	d = dict()

	[q_type, q_id] = input().split()
	d[q_id] = 0

	for i in range(q-1):
		[q_type, q_id] = input().split()
		if q_type == 'L':
			left -=1
			d[q_id] = left
		elif q_type == 'R':
			right += 1
			d[q_id] = right
		elif q_type == '?':
			p = d[q_id]
			print(min(p-left, right-p))

n=int(input())
booklist={}
a=input().split()
booklist[a[1]]=0
left=0
right=0
for i in range(n-1):
    a=input().split()
    if a[0]=="L":
        left-=1
        booklist[a[1]]=left
    elif a[0]=="R":
        right+=1
        booklist[a[1]]=right
    else:
        print(min(booklist[a[1]]-left,right-booklist[a[1]]))

leftmost = 1000000
rightmost = leftmost-1
all_books = dict()
q = int(input())
for _ in range(q):
	q_type, q_book = input().split()
	if q_type == "L":
		leftmost -= 1
		all_books[q_book] = leftmost
	elif q_type == "R":
		rightmost += 1
		all_books[q_book] = rightmost
	else:
		print(min(rightmost-all_books[q_book], all_books[q_book]-leftmost))


n = int(input())
shelf, L, R = {}, 0, 0
p, d = input().split()
shelf[d] = 0
for i in range(n-1):
    p, d = input().split()
    if p == 'L':
        shelf[d] = L - 1
        L -= 1 
    elif  p == 'R':
        shelf[d] = R + 1
        R += 1
    else:
        a = -(L - shelf[d])        
        b = R - shelf[d] 
        print(min(a, b))



n = int(input())
d = {}
l = 0
r = 0
for i in range(n):
    s = input().split()
    if s[0] == 'R':
        d[s[1]] = [1, r]
        r += 1
    if s[0] == 'L':
        d[s[1]] = [0, l]
        l += 1
    if s[0] == '?':
        if d[s[1]][0] == 0:
            print(min(l - d[s[1]][1] - 1, d[s[1]][1] + r))
        else:
            print(min(r - d[s[1]][1] - 1, l + d[s[1]][1]))

TOT = 2*10**5
# print(TOT)
a = [-1] * (TOT + 1)

q = int(input())
fp = input().split()
a[int(fp[1])] = 0
L = -1
R = 1
for _ in range(q-1):
    qq = input().split()
    pos = int(qq[1])
    if (qq[0]=='L'):
        a[pos] = L
        L -= 1
    elif qq[0]=='R':
        a[pos] = R
        R += 1
    else:
        print(min(a[pos] - L - 1,R - a[pos] - 1))
        


import sys
f=sys.stdin
out=sys.stdout

q=int(f.readline().rstrip('\r\n'))
pos,id=f.readline().rstrip('\r\n').split()
mid=int(id)

left=[]
right=[]
dleft={}
dright={}

for i in range(q-1):
	pos,id=f.readline().rstrip('\r\n').split()
	id=int(id)
	if pos=="L":
		left.append(id)
		dleft[id]=len(dleft)+1
	elif pos=="R":
		right.append(id)
		dright[id]=len(dright)+1
	else:
		if id==mid:
			out.write(str(min(len(dleft),len(dright)))+"\n")
		elif id in dleft:
			z=len(dleft)-dleft[id]+1
			out.write(str(min(z-1,1+len(dright)+dleft[id]-1))+"\n")
		else:
			z=dright[id]
			#print(z)
			out.write(str(min(len(dleft)+z,len(dright)-z))+"\n")

mini, maxi = 1, 0
indexes = {}
ans = []
for q in range(int(input())):
	cmd = input().split()
	idx = int(cmd[1])
	if cmd[0]=="L":
		mini-=1
		indexes[idx]=mini
	elif cmd[0]=="R":
		maxi+=1
		indexes[idx]=maxi
	else:
		ind = indexes[idx]
		ans.append(min(ind-mini,maxi-ind))
print(*ans,sep="\n")


n = int(input())
arr = {}
mi = 0
ma = 0
t, i = input().split()
arr[i] = 0
for _ in range(n-1):
	t, i = input().split()
	if t == 'L':
		mi -= 1
		arr[i] = mi
	elif t == 'R':
		ma += 1
		arr[i] = ma
	else:
		print(min(ma-arr[i], arr[i]-mi))


q = int(input())
minimum = 0
maximum = -1
d = {}
for i in range(q):
    a, b = input().split()
    b = int(b)
    if a == 'L':
        minimum -= 1
        d[b] = minimum
    elif a == 'R':
        maximum += 1
        d[b] = maximum
    elif a == '?':
        print(min(d[b] - minimum, maximum - d[b]))
        


q = int(input())
lm = 0
rm = 0
idx = {}
ans = []
for i in range(q):
    c,id = [s for s in input().split()]
    if i == 0:
        idx[id] = 0
    else:
        if c == 'L':
            lm -= 1
            idx[id] = lm
        elif c == 'R':
            rm += 1
            idx[id] = rm
        elif c == '?':
            ans.append(min(idx[id]-lm, rm-idx[id]))
print(*ans, sep='\n')

n=int(input())
ln=0
rn=0
ids={}
bel={}

for i in range(n):
    s=input().split()
    op=s[0]
    id=int(s[1])

    if op=='L' or op=='R':
        if op=='L':
            ln+=1
            ids[id]=ln
            bel[id]='l'
        else:
            rn+=1
            ids[id]=rn
            bel[id]='r'
    else:
        if bel[id]=='l':
             l=ln-ids[id]
             r=ids[id]+rn-1
             print(min(l,r))
        else:
            r=rn-ids[id]
            l=ids[id]+ln-1
            print(min(l,r))


    # if op=='L' or op=='R':
    #     if op=='L':
    #         ln+=1
    #         ids[id]=-ln
    #     else:
    #         rn+=1
    #         ids[id]=rn
    # else:
    #     if ids[id]<0:
    #         ll=ln-abs(ids[id])+1
    #         rr=abs(ids[id])+rn
    #         print(min(ll,rr)-1)
    #     else:
    #         rr=rn-abs(ids[id])+1
    #         ll=abs(ids[id])+rn
    #         print(min(ll,rr)-1)


import sys

q=int(sys.stdin.readline())
Q=[sys.stdin.readline().split() for i in range(q)]

Lcount=0
Rcount=0
BOOK=[None]*(2*10**5+1)
for i in range(q):
    if Q[i][0]=="L":
        Lcount+=1
        BOOK[int(Q[i][1])]=("L",Lcount,Rcount)
        
        
    elif Q[i][0]=="R":
        Rcount+=1
        BOOK[int(Q[i][1])]=("R",Lcount,Rcount)
        

    else:
        l=BOOK[int(Q[i][1])][1]
        r=BOOK[int(Q[i][1])][2]
        #print(l,r)
        if BOOK[int(Q[i][1])][0]=="R":
            ANS=min(Rcount-r,Lcount-l+(r+l-1))
        elif BOOK[int(Q[i][1])][0]=="L":
            ANS=min(Lcount-l,Rcount-r+(r+l-1))


        sys.stdout.write(str(ANS)+"\n")

#print(Lcount)
        


#!/usr/bin/env python3
import sys

def rint():
    return list(map(int, sys.stdin.readline().split()))
#lines = stdin.readlines()

q = int(input())

order = [0]*(2*10**5+1)
lm_order = 0
rm_order = 0

not_init = 1
for i in range(q):
    cmd, id = sys.stdin.readline().split()
    id = int(id)
    if cmd == 'L':
        order[id] = lm_order - 1
        lm_order = lm_order - 1
        if not_init:
            rm_order = lm_order
            not_init = 0
    elif cmd == 'R':
        order[id] = rm_order + 1
        rm_order = rm_order + 1
        if not_init:
            lm_order = rm_order
            not_init = 0
    else:
        print(min(order[id]- lm_order, rm_order - order[id]))



'''input
10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115
'''
from sys import stdin, stdout

def  myfunction(mydict, current, second):
	a = mydict[second]
	if a[2] == 'R':
		return min( current[1] - a[1], current[0] + a[1] - 1)
	else:
		return min(current[0] - a[0], current[1] + a[0] - 1)

q = int(stdin.readline())
mydict = dict()
current = [0, 0, 0]

while q > 0:
	
	first, second = stdin.readline().split()
	if first == 'L':
		mydict[second] = [current[0] + 1, current[1], 'L']
		current = [current[0] + 1, current[1], 'L']
	elif first == 'R':
		mydict[second] = [current[0], current[1] + 1, 'R']
		current = [current[0], current[1] + 1, 'R']
	else:
		print(myfunction(mydict, current, second))
	#print(current)

	q -= 1

# use this as the main template for python problems
import sys

def solution(q):

    placed = False;
    lind = 0;
    rind = 0;
    book_map = {};
    for i in range(q):
        
        op, book_ind = [val for val in sys.stdin.readline().split()]
        book_ind = int(book_ind)

        if(op == 'R'):
            book_map[book_ind] = rind;
            rind += 1;
            if(not placed):
                placed = True;
                lind -= 1;

        elif(op == 'L'):
            book_map[book_ind] = lind;
            lind -= 1;
            if(not placed):
                placed = True;
                rind += 1;
            
        elif(op == '?'):
            ldist = book_map[book_ind] - lind;
            rdist =  rind - book_map[book_ind];
            print((min(ldist, rdist) - 1));


def __starting_point():

    q = [int(val) for val in sys.stdin.readline().split()][0]
    solution(q);
    





__starting_point()

import collections
arr = [0]*200010
query = int(input())
total = 0
l = 0
r = 0
ans = []
for test in range(query):
	ty,idd = [(x) for x in input().split()]
	idd = int(idd)
	if ty=='L':
		l+=1
		arr[idd] = (1,l-1)
		total+=1
	elif ty=='R':
		r+=1
		arr[idd] = (0,r-1)
		total+=1
	else:	
		left = 0
		right = 0
		x = arr[idd]
		if x[0]==0:
			left = l+x[1]
			right = r-x[1]-1
		else:
			left = l-x[1]-1
			right = r+x[1]
		ans.append(min(left,right))
	#print(arr)
for i in range(len(ans)):
	print(ans[i])

n = int(input())
s, e = 0, 0
c = True
w = dict()
for _ in range(n):
    q, d = input().split()
    if q == "R":
        if c:
            s += 1
            c = False
        e += 1
        w[d] = e
    elif q == "L":
        if c:
            e -= 1
            c = False
        s -= 1
        w[d] = s
    else:
        i = w[d]
        if i > e:
            i -= n
        print(min(e - i, i - s))


n = int(input())
r = l = 0
right_pos = {}
left_pos = {}
for _ in range(n):
    com,id = input().split()
    if com == 'R':
        right_pos[id] = r
        r += 1
    elif com == 'L':
        left_pos[id] = l
        l += 1
    else:
        if id in right_pos:
            ind = right_pos[id]
            output = min(l+ind,r-ind-1)
            print(output)
        else:
            ind = left_pos[id]
            output = min(l-ind-1,ind+r)
            print(output)

q = int(input())
l = 0
r = 0
all_ = 0
arr = [0] * (1000000 * 2)
pos = [0] * 1000000
for i in range(q):
    s = input().split()
    id_ = int(s[1])
    op = s[0]
    if op == 'L':
        if all_ == 0:
            l = 1000001
            r = l + 1
        arr[l] = id_
        pos[id_] = l
        l -= 1
        all_ += 1
    if op == 'R':
        if all_ == 0:
            r = 1000001
            l = r - 1
        arr[r] = id_
        pos[id_] = r
        r += 1
        all_ += 1
    if op == '?':
        print(min(pos[id_] - l, r - pos[id_]) - 1)

q = int(input())
dc = dict()
l = 0
r = 0
z, n = input().split()
n = int(n)
dc[n] = l
for i in range(q - 1):
    z, n = input().split()
    n = int(n)
    if z == 'L':
        l -= 1
        dc[n] = l
    elif z == 'R':
        r += 1
        dc[n] = r
    else:
        print(min(dc[n] - l, r - dc[n]))


# coding=utf-8

q = int(input())

a = [0 for i in range(2 * 10 ** 5 + 1)]
base = 0
l = -1
r = 1
input()
k = 0

for i in range(q - 1):
    s = input().split()
    b = int(s[1])

    if s[0] == 'L':
        a[b] = l
        l -= 1
        base += 1
        k += 1
    elif s[0] == 'R':
        a[b] = r
        r += 1
        k += 1
    else:
        ind = a[b] + base
        print(min(ind, k - ind))