#D1238. Deque

    ID: 1031 Type: Default 2000ms 268MiB

Deque

Deque

For a dynamic array A=a0,a1,...A = \\{a_0, a_1, ...\\} of integers, perform a sequence of the following operations:

  • push(dd, xx): Add element xx at the begining of AA, if d=0d = 0. Add element xx at the end of AA, if d=1d = 1.
  • randomAccess(pp): Print element apa_p.
  • pop(dd): Delete the first element of AA, if d=0d = 0. Delete the last element of AA, if d=1d = 1.

AA is a 0-origin array and it is empty in the initial state.

Constraints

  • 1q400,0001 \leq q \leq 400,000
  • 0p<0 \leq p < the size of AA
  • 1,000,000,000x1,000,000,000-1,000,000,000 \leq x \leq 1,000,000,000

Input

The input is given in the following format.

qq query1query_1 query2query_2 : queryqquery_q

Each query queryiquery_i is given by

0 dd xx

or

1 pp

or

2 dd

where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.

randomAccess and pop operations will not be given for an empty array.

Output

For each randomAccess, print apa_p in a line.

Example

Input

11 0 0 1 0 0 2 0 1 3 1 0 1 1 1 2 2 0 2 1 0 0 4 1 0 1 1

Output

2 1 3 4 1

inputFormat

Input

The input is given in the following format.

qq query1query_1 query2query_2 : queryqquery_q

Each query queryiquery_i is given by

0 dd xx

or

1 pp

or

2 dd

where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.

randomAccess and pop operations will not be given for an empty array.

outputFormat

Output

For each randomAccess, print apa_p in a line.

Example

Input

11 0 0 1 0 0 2 0 1 3 1 0 1 1 1 2 2 0 2 1 0 0 4 1 0 1 1

Output

2 1 3 4 1

样例

11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
2

1 3 4 1

</p>