Bookshelf#201

Bookshelf

1 second
128 megabytes
Easy (≈1000)

Mammad stacks his books on a shelf. If there are no books on the shelf, he simply places it on the shelf; if there are, he places it either to the right or left of the already stacked books. He also picks up books from the same side, either from the right or left. Based on the given information, you need to model Mammad's actions and provide the numbers of the books he will pick up.

Input

The first line provides the number n~(1 \le n \le 10000), representing the number of operations performed by Mammad. Each of the following lines contains information about the operations. Each book placement operation is represented by two numbers.

The first number ( or ) indicates whether the book will be placed on the left or right side, respectively, and the second integer (ranging from to ) is the book's number. The operation of picking up a book from the shelf is expressed by two numbers ( or ), corresponding to picking up from the left or right side, respectively.

Output

For each book removal operation, provide the number of the book that was removed.

Examples

Example 1

Loading
Loading