Under construction

Still being built. Things may change

Deque 2

Time limit2sMemory limit1024 MB

Summary
The task is to implement a deque with eight command types (push front/back, pop front/back, size, empty, front, back) and print results for queries.
Level

Easy2 of 10

Topics
Queue, Simulation
Solved
No attempts yet

Problem

Implement a deque that stores integers, then write a program that processes the commands given as input.

There are eight kinds of commands.

  1. 1 X: Inserts the integer X at the front of the deque. (1 ≤ X ≤ 100,000)
  2. 2 X: Inserts the integer X at the back of the deque. (1 ≤ X ≤ 100,000)
  3. 3: If the deque has an integer, removes the front integer and prints it. Otherwise, prints -1.
  4. 4: If the deque has an integer, removes the back integer and prints it. Otherwise, prints -1.
  5. 5: Prints the number of integers in the deque.
  6. 6: Prints 1 if the deque is empty, otherwise 0.
  7. 7: If the deque has an integer, prints the front integer. Otherwise, prints -1.
  8. 8: If the deque has an integer, prints the back integer. Otherwise, prints -1.

Input

The first line contains the number of commands N. (1 ≤ N ≤ 1,000,000)

Each of the following N lines contains one command.

At least one command asks for output.

Output

For each command that asks for output, print the result on its own line.

Examples1

  1. Example 1

    Input
    11
    6
    1 3
    1 8
    7
    8
    3
    2 5
    1 2
    5
    4
    4
    
    Expected output
    1
    8
    3
    8
    3
    5
    3