# -*- coding: utf-8 -*-

"""

Created on Sat May 19 15:10:55 2018

AOJ ITP2_1_B

@author: maezawa

"""

#import numpy as np

q = int(eval(input()))

a = [0 for _ in range(8000000)]

start = 4000000

end = 4000001

for i in range(q):

    ops = list(map(int,input().split()))

    if ops[0] == 0:

        if ops[1] == 0:

            a[start] = ops[2]

            start -= 1

        else:

            a[end] = ops[2]

            end += 1

    elif ops[0] == 1:

        print((a[start+ops[1]+1]))

    elif ops[0] == 2:

        if ops[1] == 0:

            start += 1

        elif ops[1] == 1:

            end -= 1

#    print(a[start+1:end])

# -*- coding: utf-8 -*-

"""

Created on Sun Jun 24 14:53:50 2018

ITP21B

@author: maezawa

"""



from collections import deque



d = deque()



n = int(eval(input()))



for i in range(n):

    c = list(map(int, input().split()))

    if c[0] == 0:

        if c[1] == 0:

            d.appendleft(c[2])

        else:

            d.append(c[2])

    elif c[0] == 1:

        print((d[c[1]]))

    else:

        if c[1] == 0:

            d.popleft()

        else:

            d.pop()





# -*- coding: utf-8 -*-
"""
Created on Sat May 19 15:10:55 2018
AOJ ITP2_1_B
@author: maezawa
"""
# import numpy as np
q = int(eval(input()))
a = [0 for _ in range(8000000)]
start = 4000000
end = 4000001
for i in range(q):
    ops = list(map(int, input().split()))
    if ops[0] == 0:
        if ops[1] == 0:
            a[start] = ops[2]
            start -= 1
        else:
            a[end] = ops[2]
            end += 1
    elif ops[0] == 1:
        print((a[start + ops[1] + 1]))
    elif ops[0] == 2:
        if ops[1] == 0:
            start += 1
        elif ops[1] == 1:
            end -= 1
#    print(a[start+1:end])

# -*- coding: utf-8 -*-
"""
Created on Sun Jun 24 14:53:50 2018
ITP21B
@author: maezawa
"""
from collections import deque

d = deque()
n = int(eval(input()))
for i in range(n):
    c = list(map(int, input().split()))
    if c[0] == 0:
        if c[1] == 0:
            d.appendleft(c[2])
        else:
            d.append(c[2])
    elif c[0] == 1:
        print((d[c[1]]))
    else:
        if c[1] == 0:
            d.popleft()
        else:
            d.pop()

-Created on Sat May 19 15:10:55 2018 -AOJ ITP2_1_B +Created on Sun Jun 24 14:53:50 2018 +ITP21B -# import numpy as np -q = int(eval(input())) -a = [0 for _ in range(8000000)] -start = 4000000 -end = 4000001 -for i in range(q): -    ops = list(map(int, input().split())) -    if ops[0] == 0: -        if ops[1] == 0: -            a[start] = ops[2] -            start -= 1 +from collections import deque + +d = deque() +n = int(eval(input())) +for i in range(n): +    c = list(map(int, input().split())) +    if c[0] == 0: +        if c[1] == 0: +            d.appendleft(c[2]) -            a[end] = ops[2] -            end += 1 -    elif ops[0] == 1: -        print((a[start + ops[1] + 1])) -    elif ops[0] == 2: -        if ops[1] == 0: -            start += 1 -        elif ops[1] == 1: -            end -= 1 -#    print(a[start+1:end]) +            d.append(c[2]) +    elif c[0] == 1: +        print((d[c[1]])) +    else: +        if c[1] == 0: +            d.popleft() +        else: +            d.pop()