This page is still under construction.

Parts of this page are still being built. What you see may change.

Time Travel

Time limit1sMemory limit128 MB

Summary
Process add, pop, and rewind-to-earlier-query operations on a recorded list, printing the last element after each query.
Level

Medium6 of 10

Topics
Stack, Tree, Implementation, DFS
Solved
No attempts yet

Problem

Hyunsu bought a time machine to get more coding practice time. At each step, Hyunsu does exactly one of the following:

  1. Solve a problem as usual, without using the time machine.
  2. Time-travel to some point in the past.

He can never travel to the future, and once he goes back to the past, a new future starts unfolding from that point.

Hyunsu records the problems he solves, in the order he solves them. When he travels to the past, only the problems recorded up to (but not including) that point remain in his list.

At every step, Hyunsu wants to know the number of the most recently solved problem in the list. If the list is empty, print −1-1.

Hyunsu processes NN (1≤N≤80,0001 \le N \le 80{,}000) queries Q1,Q2,…,QNQ_1, Q_2, \dots, Q_N in order along his timeline. Each query is given on its own line and starts with a character cc (one of 'a', 's', 't').

  • If cc is 'a', it is followed by a space and an integer KK (1≤K≤1,000,0001 \le K \le 1{,}000{,}000). Hyunsu solves problem number KK and appends it to the end of the list.
  • If cc is 's', Hyunsu removes the most recently recorded problem from the list.
  • If cc is 't', it is followed by a space and an integer KK. Hyunsu time-travels to the state just before the KK-th query is processed; that is, he returns to the list as it was after processing the first K−1K-1 queries. Since he cannot travel to the future, KK is at most the number of the current query.

After processing each query QiQ_i, print the number of the most recently solved problem remaining in the list, or −1-1 if the list is empty.

For example, processing the queries 'a 5', 'a 8', 's', 't 2', 'a 9' in order changes the list as [5]→[5,8]→[5]→[5]→[5,9][5] \to [5, 8] \to [5] \to [5] \to [5, 9], and the outputs are 5,8,5,5,95, 8, 5, 5, 9. Here 't 2' returns to the state [5][5], which is the list just before the 2nd query (i.e., after processing only the 1st query).

Input

The first line contains the number of queries NN.

Each of the next NN lines (from the 2nd line through the (N+1)(N+1)-th line) contains one query QiQ_i. Each query has one of the forms 'a KK', 's', or 't KK'.

Output

After processing each query QiQ_i, print the number of the most recently solved problem remaining in the list, one per line. If there is no such problem, print −1-1.

Examples1

  1. Example 1

    Input
    12
    a 5
    a 3
    a 7
    s
    t 2
    a 2
    t 4
    a 4
    s
    t 7
    s
    s
    
    Expected output
    5
    3
    7
    3
    5
    2
    7
    4
    7
    2
    5
    -1