Under construction

Still being built. Things may change

Veenus

Interview

Time limit1sMemory limit1024 MB

Summary
Given a rooted tree and a stream of toggles on node danger levels, report the lowest common ancestor of all currently high-danger nodes after each toggle.
Level

Medium6 of 10

Topics
Tree, Sorting, Binary search
Solved
No attempts yet

Problem

Veenus has a hierarchical administrative division. The planet is split into regions, each region may be split into subregions, each subregion into sub-subregions, and so on. The planet has NN administrative units numbered 11 to NN, where unit 11 is the whole planet. So the administrative division of Veenus forms a tree rooted at 11.

Veenus has many volcanoes, and its residents worry constantly about possible eruptions. Each unit therefore has a volcanic activity response center. A center is activated when its unit declares a high danger level. A unit can have a high danger level while all of its subunits have low danger levels.

When several response centers are active at once, their work must be coordinated. The smallest unit that contains all units with a high danger level is put in charge of the situation. Each unit is considered strictly larger than all of its subunits, even when it has only one subunit.

Volcanic activity changes often. The program must receive reports that a unit's danger level changed from low to high or from high to low, and after each report it must find the unit that should now be in charge of the situation.

Input

The first line contains the number of administrative units NN (1≤N≤1051 \le N \le 10^5).

Each of the next NN lines describes one unit. Line 1+i1 + i starts with the number of subunits KiK_i of unit ii (0≤Ki≤N−10 \le K_i \le N - 1), followed by KiK_i integers Ai,jA_{i,j} (1≤Ai,j≤N1 \le A_{i,j} \le N). These mean that units Ai,1,Ai,2,…,Ai,KiA_{i,1}, A_{i,2}, \ldots, A_{i,K_i} are subunits of unit ii.

The next line contains the number of reports QQ (1≤Q≤1051 \le Q \le 10^5).

Each of the next QQ lines contains a report, given as integers TiT_i and ViV_i (1≤Ti≤21 \le T_i \le 2, 1≤Vi≤N1 \le V_i \le N). Ti=1T_i = 1 means that the danger level of unit ViV_i changed from low to high, and Ti=2T_i = 2 means that it changed from high to low.

All units have a low danger level at the start, and the input is consistent. A report that a unit changed to high is only given when its level was low before, and vice versa.

Output

Print QQ lines, each with one integer. The ii-th line is the number of the unit in charge after processing the report on line N+2+iN + 2 + i of the input. If all units have a low danger level, print 00.

Examples3

  1. Example 1

    Input
    7
    2 2 3
    2 4 5
    2 6 7
    0
    0
    0
    0
    10
    1 1
    2 1
    1 4
    1 5
    1 6
    1 7
    2 5
    2 4
    2 7
    1 3
    
    Expected output
    1
    0
    4
    2
    1
    1
    1
    3
    6
    3
    
  2. Example 2

    Input
    4
    1 2
    1 4
    0
    1 3
    4
    1 4
    1 3
    1 1
    2 1
    
    Expected output
    4
    4
    1
    4
    
  3. Example 3

    Input
    5
    4 2 3 4 5
    0
    0
    0
    0
    6
    1 2
    1 3
    2 2
    1 5
    1 2
    2 5
    
    Expected output
    2
    1
    3
    1
    1
    1