k-th ancestor#9629

k-th ancestor

4 seconds
128 megabytes
Easy

A tree with vertices is a connected undirected graph with p - 1 edges. Let be the root of the tree. If a vertex is at distance from , a vertex is at distance l + 1 from , and is connected to by an edge, then is called the parent of .

Similarly, if is at distance from , is at distance l + k from , and there is a path of length from to , then is called the -th parent of .

Susan likes to play with graphs, and trees are one of her favorite data structures. She has designed a problem and wants to know whether anyone can solve it. From time to time, she adds a new leaf to the tree or removes an existing one. Your task is to find the -th parent of a given vertex at any moment.

Input

The first line contains an integer t~(1 \le t \le 3) — the number of test cases. The test cases follow.

The first line of each test case contains an integer p~(1 \le p \le 10^5) — the number of vertices in the tree. Each of the next lines contains two integers and y~(1 \le x \le 10^5, 0 \le y \le 10^5), meaning that is the parent of . If y = 0, then is the root of the tree ( is only a placeholder and is not a vertex of the tree).

The next line contains an integer q~(1 \le q \le 10^5) — the number of queries. Each of the next lines contains one query of one of the following types:

  • — add a new leaf whose parent is . It is guaranteed that is not in the tree and is in the tree.

  • — remove the leaf from the tree. It is guaranteed that is a leaf of the tree.

  • 2~x~k~(1 \le k \le 10^5) — find the -th parent of .

All vertex numbers in the queries do not exceed 10^5.

Output

For each query of type , print the -th parent of on a separate line. If the -th parent does not exist or the vertex is not in the tree, print .

Examples

Example 1

Loading
Loading