Problem 04B. Queue from Two Stacks ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

Implement a FIFO queue supporting n operations of two kinds:

It is guaranteed that POP is never called on an empty queue. You must use only a Stack ADT (push / pop / top from one end) as your underlying storage — no deque, no list indexing from the front, no built-in queue.

Input format

The first line contains one integer n — the number of operations. Each of the next n lines is either PUSH x or POP.

Output format

For each POP, print the removed value on its own line, in order.

Constraints

1 ≤ n ≤ 2 ⋅ 105

1 ≤ x ≤ 109

Sample tests

No. Standard input Standard output
1
8
PUSH 1
PUSH 2
POP
PUSH 3
POP
POP
PUSH 4
POP
1
2
3
4
2
2
PUSH 42
POP
42
3
6
PUSH 1
PUSH 2
PUSH 3
POP
POP
POP
1
2
3

0.035s 0.013s 16