Persistent Queue#4312

Persistent Queue

2 seconds
64 megabytes
Easy

Implement a persistent queue.

Input

The first line contains the number of operations (1 \leq n \leq 200000). The (i+1)-th line contains the description of the -th operation:

  • — add the number to the end of queue number (0 \leq t < i);

  • — remove the first element from queue number (0 \leq t < i).

As a result of the -th operation, described in the (i+1)-th line, a new queue numbered is created. Initially, there is an empty queue numbered zero.

All numbers in the input are -bit signed integers.

Output

For each remove operation, output the removed element on a separate line.

Examples

Example 1

Loading
Loading