Deques on 6 Megabytes#3161

Deques on 6 Megabytes

2 seconds
256 megabytes
Very easy

Write a program that processes a large number of deques. A deque is a "queue with two ends".

Input

The first line contains the total number of commands (0 \leq n \leq 150{,}000). Each of the next lines contains a command description:

  • pushfront A B — insert the number at the front of deque ;

  • pushback A B — insert the number at the back of deque ;

  • popfront A — remove the first element from deque ;

  • popback A — remove the last element from deque .

In each command, and are integers from to inclusive.

Output

For each popfront or popback command, print the removed number. It is guaranteed that before every remove operation the corresponding deque is not empty.

Examples

Example 1

Loading
Loading