#abc295g. [abc295_g]Minimum Reachable City

[abc295_g]Minimum Reachable City

Problem Statement

We have a directed graph GSG_S with NN vertices numbered 11 to NN. It has N1N-1 edges. The ii-th edge (1leqileqN1)(1\\leq i \\leq N-1) goes from vertex pi(1leqpileqi)p_i\\ (1\\leq p_i \\leq i) to vertex i+1i+1.

We have another directed graph GG with NN vertices numbered 11 to NN. Initially, GG equals GSG_S. Process QQ queries on GG in the order they are given. There are two kinds of queries as follows.

  • 1 u v : Add an edge to GG that goes from vertex uu to vertex vv. It is guaranteed that the following conditions are satisfied.
    • uneqvu \\neq v.
    • On GSG_S, vertex uu is reachable from vertex vv via some edges.
  • 2 x : Print the smallest vertex number of a vertex reachable from vertex xx via some edges on GG (including vertex xx).

Constraints

  • 2leqNleq2times1052\\leq N \\leq 2\\times 10^5
  • 1leqQleq2times1051\\leq Q \\leq 2\\times 10^5
  • 1leqpileqi1\\leq p_i\\leq i
  • For each query in the first format:
    • 1lequ,vleqN1\\leq u,v \\leq N.
    • uneqvu \\neq v.
    • On GSG_S, vertex uu is reachable from vertex vv via some edges.
  • For each query in the second format, 1leqxleqN1\\leq x \\leq N.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN p1p_1 p2p_2 dots\\dots pN1p_{N-1} QQ mathrmquery1\\mathrm{query}_1 mathrmquery2\\mathrm{query}_2 vdots\\vdots mathrmqueryQ\\mathrm{query}_Q

Here, mathrmqueryi\\mathrm{query}_i denotes the ii-th query and is in one of the following formats:

11 uu vv 22 xx

Output

Print qq lines, where qq is the number of queries in the second format. The ii-th line (1leqjleqq)(1\\leq j \\leq q) should contain the answer to the jj-th query in the second format.


Sample Input 1

5
1 2 3 3
5
2 4
1 4 2
2 4
1 5 1
2 4

Sample Output 1

4
2
1
  • At the time of the first query, only vertex 44 is reachable from vertex 44 via some edges on GG.
  • At the time of the third query, vertices 22, 33, 44, and 55 are reachable from vertex 44 via some edges on GG.
  • At the time of the fifth query, vertices 11, 22, 33, 44, and 55 are reachable from vertex 44 via some edges on GG.

Sample Input 2

7
1 1 2 2 3 3
10
2 5
1 5 2
2 5
1 2 1
1 7 1
1 6 3
2 5
2 6
2 1
1 7 1

Sample Output 2

5
2
1
1
1