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$.
[{"code": "n = int(input())\nd = {}\nmatr = [0] * (2 * n + 1)\nhead = n - 1\ntail = n\nfor i in range(n):\n\tst, n = input().split()\n\tn = int(n)\n\tif st == 'L':\n\t\tmatr[head] = n\n\t\td[n] = head\n\t\thead -= 1\n\telif st == 'R':\n\t\tmatr[tail] = n\n\t\td[n] = tail\n\t\ttail += 1\n\telse:\n\t\tprint(min(d[n] - head, tail - d[n]) - 1)\n", "passed": true, "time": 0.24, "memory": 14212.0, "status": "done"}, {"code": "n=int(input())\na=[0]*200001\nmi=0\nma=0\ninput()\nfor i in range(n-1):\n    r,id=input().split()\n    id=int(id)\n    if r=='L':\n        mi-=1\n        a[id]=mi\n    elif r=='R':\n        ma+=1\n        a[id]=ma\n    else:\n        print(min(a[id]-mi,ma-a[id]))", "passed": true, "time": 0.25, "memory": 14916.0, "status": "done"}, {"code": "q = int(input())\n\nif q > 0:\n\tleft = 0\n\tright = 0\n\n\td = dict()\n\n\t[q_type, q_id] = input().split()\n\td[q_id] = 0\n\n\tfor i in range(q-1):\n\t\t[q_type, q_id] = input().split()\n\t\tif q_type == 'L':\n\t\t\tleft -=1\n\t\t\td[q_id] = left\n\t\telif q_type == 'R':\n\t\t\tright += 1\n\t\t\td[q_id] = right\n\t\telif q_type == '?':\n\t\t\tp = d[q_id]\n\t\t\tprint(min(p-left, right-p))", "passed": true, "time": 0.16, "memory": 14072.0, "status": "done"}, {"code": "n=int(input())\nbooklist={}\na=input().split()\nbooklist[a[1]]=0\nleft=0\nright=0\nfor i in range(n-1):\n    a=input().split()\n    if a[0]==\"L\":\n        left-=1\n        booklist[a[1]]=left\n    elif a[0]==\"R\":\n        right+=1\n        booklist[a[1]]=right\n    else:\n        print(min(booklist[a[1]]-left,right-booklist[a[1]]))", "passed": true, "time": 0.15, "memory": 14304.0, "status": "done"}, {"code": "leftmost = 1000000\nrightmost = leftmost-1\nall_books = dict()\nq = int(input())\nfor _ in range(q):\n\tq_type, q_book = input().split()\n\tif q_type == \"L\":\n\t\tleftmost -= 1\n\t\tall_books[q_book] = leftmost\n\telif q_type == \"R\":\n\t\trightmost += 1\n\t\tall_books[q_book] = rightmost\n\telse:\n\t\tprint(min(rightmost-all_books[q_book], all_books[q_book]-leftmost))\n", "passed": true, "time": 0.25, "memory": 14244.0, "status": "done"}, {"code": "n = int(input())\nshelf, L, R = {}, 0, 0\np, d = input().split()\nshelf[d] = 0\nfor i in range(n-1):\n    p, d = input().split()\n    if p == 'L':\n        shelf[d] = L - 1\n        L -= 1 \n    elif  p == 'R':\n        shelf[d] = R + 1\n        R += 1\n    else:\n        a = -(L - shelf[d])        \n        b = R - shelf[d] \n        print(min(a, b))\n\n", "passed": true, "time": 0.24, "memory": 14208.0, "status": "done"}, {"code": "n = int(input())\nd = {}\nl = 0\nr = 0\nfor i in range(n):\n    s = input().split()\n    if s[0] == 'R':\n        d[s[1]] = [1, r]\n        r += 1\n    if s[0] == 'L':\n        d[s[1]] = [0, l]\n        l += 1\n    if s[0] == '?':\n        if d[s[1]][0] == 0:\n            print(min(l - d[s[1]][1] - 1, d[s[1]][1] + r))\n        else:\n            print(min(r - d[s[1]][1] - 1, l + d[s[1]][1]))", "passed": true, "time": 0.15, "memory": 14016.0, "status": "done"}, {"code": "TOT = 2*10**5\n# print(TOT)\na = [-1] * (TOT + 1)\n\nq = int(input())\nfp = input().split()\na[int(fp[1])] = 0\nL = -1\nR = 1\nfor _ in range(q-1):\n    qq = input().split()\n    pos = int(qq[1])\n    if (qq[0]=='L'):\n        a[pos] = L\n        L -= 1\n    elif qq[0]=='R':\n        a[pos] = R\n        R += 1\n    else:\n        print(min(a[pos] - L - 1,R - a[pos] - 1))\n        \n", "passed": true, "time": 0.15, "memory": 15104.0, "status": "done"}, {"code": "import sys\nf=sys.stdin\nout=sys.stdout\n\nq=int(f.readline().rstrip('\\r\\n'))\npos,id=f.readline().rstrip('\\r\\n').split()\nmid=int(id)\n\nleft=[]\nright=[]\ndleft={}\ndright={}\n\nfor i in range(q-1):\n\tpos,id=f.readline().rstrip('\\r\\n').split()\n\tid=int(id)\n\tif pos==\"L\":\n\t\tleft.append(id)\n\t\tdleft[id]=len(dleft)+1\n\telif pos==\"R\":\n\t\tright.append(id)\n\t\tdright[id]=len(dright)+1\n\telse:\n\t\tif id==mid:\n\t\t\tout.write(str(min(len(dleft),len(dright)))+\"\\n\")\n\t\telif id in dleft:\n\t\t\tz=len(dleft)-dleft[id]+1\n\t\t\tout.write(str(min(z-1,1+len(dright)+dleft[id]-1))+\"\\n\")\n\t\telse:\n\t\t\tz=dright[id]\n\t\t\t#print(z)\n\t\t\tout.write(str(min(len(dleft)+z,len(dright)-z))+\"\\n\")", "passed": true, "time": 0.25, "memory": 14176.0, "status": "done"}, {"code": "mini, maxi = 1, 0\nindexes = {}\nans = []\nfor q in range(int(input())):\n\tcmd = input().split()\n\tidx = int(cmd[1])\n\tif cmd[0]==\"L\":\n\t\tmini-=1\n\t\tindexes[idx]=mini\n\telif cmd[0]==\"R\":\n\t\tmaxi+=1\n\t\tindexes[idx]=maxi\n\telse:\n\t\tind = indexes[idx]\n\t\tans.append(min(ind-mini,maxi-ind))\nprint(*ans,sep=\"\\n\")\n", "passed": true, "time": 0.14, "memory": 14280.0, "status": "done"}, {"code": "n = int(input())\narr = {}\nmi = 0\nma = 0\nt, i = input().split()\narr[i] = 0\nfor _ in range(n-1):\n\tt, i = input().split()\n\tif t == 'L':\n\t\tmi -= 1\n\t\tarr[i] = mi\n\telif t == 'R':\n\t\tma += 1\n\t\tarr[i] = ma\n\telse:\n\t\tprint(min(ma-arr[i], arr[i]-mi))\n", "passed": true, "time": 0.25, "memory": 14292.0, "status": "done"}, {"code": "q = int(input())\nminimum = 0\nmaximum = -1\nd = {}\nfor i in range(q):\n    a, b = input().split()\n    b = int(b)\n    if a == 'L':\n        minimum -= 1\n        d[b] = minimum\n    elif a == 'R':\n        maximum += 1\n        d[b] = maximum\n    elif a == '?':\n        print(min(d[b] - minimum, maximum - d[b]))\n        \n", "passed": true, "time": 1.01, "memory": 14076.0, "status": "done"}, {"code": "q = int(input())\nlm = 0\nrm = 0\nidx = {}\nans = []\nfor i in range(q):\n    c,id = [s for s in input().split()]\n    if i == 0:\n        idx[id] = 0\n    else:\n        if c == 'L':\n            lm -= 1\n            idx[id] = lm\n        elif c == 'R':\n            rm += 1\n            idx[id] = rm\n        elif c == '?':\n            ans.append(min(idx[id]-lm, rm-idx[id]))\nprint(*ans, sep='\\n')", "passed": true, "time": 0.14, "memory": 14360.0, "status": "done"}, {"code": "n=int(input())\nln=0\nrn=0\nids={}\nbel={}\n\nfor i in range(n):\n    s=input().split()\n    op=s[0]\n    id=int(s[1])\n\n    if op=='L' or op=='R':\n        if op=='L':\n            ln+=1\n            ids[id]=ln\n            bel[id]='l'\n        else:\n            rn+=1\n            ids[id]=rn\n            bel[id]='r'\n    else:\n        if bel[id]=='l':\n             l=ln-ids[id]\n             r=ids[id]+rn-1\n             print(min(l,r))\n        else:\n            r=rn-ids[id]\n            l=ids[id]+ln-1\n            print(min(l,r))\n\n\n    # if op=='L' or op=='R':\n    #     if op=='L':\n    #         ln+=1\n    #         ids[id]=-ln\n    #     else:\n    #         rn+=1\n    #         ids[id]=rn\n    # else:\n    #     if ids[id]<0:\n    #         ll=ln-abs(ids[id])+1\n    #         rr=abs(ids[id])+rn\n    #         print(min(ll,rr)-1)\n    #     else:\n    #         rr=rn-abs(ids[id])+1\n    #         ll=abs(ids[id])+rn\n    #         print(min(ll,rr)-1)\n", "passed": true, "time": 0.14, "memory": 14328.0, "status": "done"}, {"code": "import sys\n\nq=int(sys.stdin.readline())\nQ=[sys.stdin.readline().split() for i in range(q)]\n\nLcount=0\nRcount=0\nBOOK=[None]*(2*10**5+1)\nfor i in range(q):\n    if Q[i][0]==\"L\":\n        Lcount+=1\n        BOOK[int(Q[i][1])]=(\"L\",Lcount,Rcount)\n        \n        \n    elif Q[i][0]==\"R\":\n        Rcount+=1\n        BOOK[int(Q[i][1])]=(\"R\",Lcount,Rcount)\n        \n\n    else:\n        l=BOOK[int(Q[i][1])][1]\n        r=BOOK[int(Q[i][1])][2]\n        #print(l,r)\n        if BOOK[int(Q[i][1])][0]==\"R\":\n            ANS=min(Rcount-r,Lcount-l+(r+l-1))\n        elif BOOK[int(Q[i][1])][0]==\"L\":\n            ANS=min(Lcount-l,Rcount-r+(r+l-1))\n\n\n        sys.stdout.write(str(ANS)+\"\\n\")\n\n#print(Lcount)\n        \n", "passed": true, "time": 0.15, "memory": 15628.0, "status": "done"}, {"code": "#!/usr/bin/env python3\nimport sys\n\ndef rint():\n    return list(map(int, sys.stdin.readline().split()))\n#lines = stdin.readlines()\n\nq = int(input())\n\norder = [0]*(2*10**5+1)\nlm_order = 0\nrm_order = 0\n\nnot_init = 1\nfor i in range(q):\n    cmd, id = sys.stdin.readline().split()\n    id = int(id)\n    if cmd == 'L':\n        order[id] = lm_order - 1\n        lm_order = lm_order - 1\n        if not_init:\n            rm_order = lm_order\n            not_init = 0\n    elif cmd == 'R':\n        order[id] = rm_order + 1\n        rm_order = rm_order + 1\n        if not_init:\n            lm_order = rm_order\n            not_init = 0\n    else:\n        print(min(order[id]- lm_order, rm_order - order[id]))\n\n", "passed": true, "time": 0.14, "memory": 15152.0, "status": "done"}, {"code": "'''input\n10\nL 100\nR 100000\nR 123\nL 101\n? 123\nL 10\nR 115\n? 100\nR 110\n? 115\n'''\nfrom sys import stdin, stdout\n\ndef  myfunction(mydict, current, second):\n\ta = mydict[second]\n\tif a[2] == 'R':\n\t\treturn min( current[1] - a[1], current[0] + a[1] - 1)\n\telse:\n\t\treturn min(current[0] - a[0], current[1] + a[0] - 1)\n\nq = int(stdin.readline())\nmydict = dict()\ncurrent = [0, 0, 0]\n\nwhile q > 0:\n\t\n\tfirst, second = stdin.readline().split()\n\tif first == 'L':\n\t\tmydict[second] = [current[0] + 1, current[1], 'L']\n\t\tcurrent = [current[0] + 1, current[1], 'L']\n\telif first == 'R':\n\t\tmydict[second] = [current[0], current[1] + 1, 'R']\n\t\tcurrent = [current[0], current[1] + 1, 'R']\n\telse:\n\t\tprint(myfunction(mydict, current, second))\n\t#print(current)\n\n\tq -= 1", "passed": true, "time": 0.14, "memory": 14420.0, "status": "done"}, {"code": "# use this as the main template for python problems\nimport sys\n\ndef solution(q):\n\n    placed = False;\n    lind = 0;\n    rind = 0;\n    book_map = {};\n    for i in range(q):\n        \n        op, book_ind = [val for val in sys.stdin.readline().split()]\n        book_ind = int(book_ind)\n\n        if(op == 'R'):\n            book_map[book_ind] = rind;\n            rind += 1;\n            if(not placed):\n                placed = True;\n                lind -= 1;\n\n        elif(op == 'L'):\n            book_map[book_ind] = lind;\n            lind -= 1;\n            if(not placed):\n                placed = True;\n                rind += 1;\n            \n        elif(op == '?'):\n            ldist = book_map[book_ind] - lind;\n            rdist =  rind - book_map[book_ind];\n            print((min(ldist, rdist) - 1));\n\n\ndef __starting_point():\n\n    q = [int(val) for val in sys.stdin.readline().split()][0]\n    solution(q);\n    \n\n\n\n\n\n__starting_point()", "passed": true, "time": 0.14, "memory": 14160.0, "status": "done"}, {"code": "import collections\narr = [0]*200010\nquery = int(input())\ntotal = 0\nl = 0\nr = 0\nans = []\nfor test in range(query):\n\tty,idd = [(x) for x in input().split()]\n\tidd = int(idd)\n\tif ty=='L':\n\t\tl+=1\n\t\tarr[idd] = (1,l-1)\n\t\ttotal+=1\n\telif ty=='R':\n\t\tr+=1\n\t\tarr[idd] = (0,r-1)\n\t\ttotal+=1\n\telse:\t\n\t\tleft = 0\n\t\tright = 0\n\t\tx = arr[idd]\n\t\tif x[0]==0:\n\t\t\tleft = l+x[1]\n\t\t\tright = r-x[1]-1\n\t\telse:\n\t\t\tleft = l-x[1]-1\n\t\t\tright = r+x[1]\n\t\tans.append(min(left,right))\n\t#print(arr)\nfor i in range(len(ans)):\n\tprint(ans[i])", "passed": true, "time": 0.14, "memory": 15156.0, "status": "done"}, {"code": "n = int(input())\ns, e = 0, 0\nc = True\nw = dict()\nfor _ in range(n):\n    q, d = input().split()\n    if q == \"R\":\n        if c:\n            s += 1\n            c = False\n        e += 1\n        w[d] = e\n    elif q == \"L\":\n        if c:\n            e -= 1\n            c = False\n        s -= 1\n        w[d] = s\n    else:\n        i = w[d]\n        if i > e:\n            i -= n\n        print(min(e - i, i - s))\n", "passed": true, "time": 0.14, "memory": 14284.0, "status": "done"}, {"code": "n = int(input())\nr = l = 0\nright_pos = {}\nleft_pos = {}\nfor _ in range(n):\n    com,id = input().split()\n    if com == 'R':\n        right_pos[id] = r\n        r += 1\n    elif com == 'L':\n        left_pos[id] = l\n        l += 1\n    else:\n        if id in right_pos:\n            ind = right_pos[id]\n            output = min(l+ind,r-ind-1)\n            print(output)\n        else:\n            ind = left_pos[id]\n            output = min(l-ind-1,ind+r)\n            print(output)", "passed": true, "time": 0.13, "memory": 14172.0, "status": "done"}, {"code": "q = int(input())\nl = 0\nr = 0\nall_ = 0\narr = [0] * (1000000 * 2)\npos = [0] * 1000000\nfor i in range(q):\n    s = input().split()\n    id_ = int(s[1])\n    op = s[0]\n    if op == 'L':\n        if all_ == 0:\n            l = 1000001\n            r = l + 1\n        arr[l] = id_\n        pos[id_] = l\n        l -= 1\n        all_ += 1\n    if op == 'R':\n        if all_ == 0:\n            r = 1000001\n            l = r - 1\n        arr[r] = id_\n        pos[id_] = r\n        r += 1\n        all_ += 1\n    if op == '?':\n        print(min(pos[id_] - l, r - pos[id_]) - 1)", "passed": true, "time": 0.18, "memory": 36992.0, "status": "done"}, {"code": "q = int(input())\ndc = dict()\nl = 0\nr = 0\nz, n = input().split()\nn = int(n)\ndc[n] = l\nfor i in range(q - 1):\n    z, n = input().split()\n    n = int(n)\n    if z == 'L':\n        l -= 1\n        dc[n] = l\n    elif z == 'R':\n        r += 1\n        dc[n] = r\n    else:\n        print(min(dc[n] - l, r - dc[n]))\n", "passed": true, "time": 0.14, "memory": 14068.0, "status": "done"}, {"code": "# coding=utf-8\n\nq = int(input())\n\na = [0 for i in range(2 * 10 ** 5 + 1)]\nbase = 0\nl = -1\nr = 1\ninput()\nk = 0\n\nfor i in range(q - 1):\n    s = input().split()\n    b = int(s[1])\n\n    if s[0] == 'L':\n        a[b] = l\n        l -= 1\n        base += 1\n        k += 1\n    elif s[0] == 'R':\n        a[b] = r\n        r += 1\n        k += 1\n    else:\n        ind = a[b] + base\n        print(min(ind, k - ind))", "passed": true, "time": 0.23, "memory": 15120.0, "status": "done"}]