#P1790F. Timofey and Black-White Tree

    ID: 8196 Type: RemoteJudge 4000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 7 Uploaded By: Tags>brute forcedfs and similardivide and conquergraphsgreedymathshortest pathstrees*2100

Timofey and Black-White Tree

No submission language available for this problem.

Description

Timofey came to a famous summer school and found a tree on nn vertices. A tree is a connected undirected graph without cycles.

Every vertex of this tree, except c0c_0, is colored white. The vertex c0c_0 is colored black.

Timofey wants to color all the vertices of this tree in black. To do this, he performs n1n - 1 operations. During the ii-th operation, he selects the vertex cic_i, which is currently white, and paints it black.

Let's call the positivity of tree the minimum distance between all pairs of different black vertices in it. The distance between the vertices vv and uu is the number of edges on the path from vv to uu.

After each operation, Timofey wants to know the positivity of the current tree.

The first line contains the integer tt (1t1041 \le t \le 10^4) — the number of testcases.

The first line of each testcase contains the integers n,c0n, c_0 (2n21052 \le n \le 2 \cdot 10^5, 1c0n1 \le c_0 \le n) — the number of vertices in the tree and index of the initial black vertex.

The second line of each testcase contains n1n - 1 unique integers c1,c2,,cn1c_1, c_2, \dots, c_{n-1} (1cin1 \le c_i \le n, cic0c_i \ne c_0), where cic_i is the vertex which is colored black during the ii-th operation.

Each of the next n1n - 1 row of each testcase contains the integers vi,uiv_i, u_i (1vi,uin1 \le v_i, u_i \le n) — edges in the tree.

It is guaranteed that the sum of nn for all testcases does not exceed 21052 \cdot 10^5.

For each testcase, print n1n - 1 integer on a separate line.

The integer with index ii must be equal to positivity of the tree obtained by the first ii operations.

Input

The first line contains the integer tt (1t1041 \le t \le 10^4) — the number of testcases.

The first line of each testcase contains the integers n,c0n, c_0 (2n21052 \le n \le 2 \cdot 10^5, 1c0n1 \le c_0 \le n) — the number of vertices in the tree and index of the initial black vertex.

The second line of each testcase contains n1n - 1 unique integers c1,c2,,cn1c_1, c_2, \dots, c_{n-1} (1cin1 \le c_i \le n, cic0c_i \ne c_0), where cic_i is the vertex which is colored black during the ii-th operation.

Each of the next n1n - 1 row of each testcase contains the integers vi,uiv_i, u_i (1vi,uin1 \le v_i, u_i \le n) — edges in the tree.

It is guaranteed that the sum of nn for all testcases does not exceed 21052 \cdot 10^5.

Output

For each testcase, print n1n - 1 integer on a separate line.

The integer with index ii must be equal to positivity of the tree obtained by the first ii operations.

Sample Input 1

6
6 6
4 1 3 5 2
2 4
6 5
5 3
3 4
1 3
4 2
4 1 3
3 1
2 3
1 4
10 3
10 7 6 5 2 9 8 1 4
1 2
1 3
4 5
4 3
6 4
8 7
9 8
10 8
1 8
7 3
7 5 1 2 4 6
1 2
3 2
4 5
3 4
6 5
7 6
9 7
9 3 1 4 2 6 8 5
4 1
8 9
4 8
2 6
7 3
2 4
3 5
5 4
10 2
1 8 5 10 6 9 4 3 7
10 7
7 8
3 6
9 7
7 6
4 2
1 6
7 5
9 2

Sample Output 1

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

Note

In the first testcase, after the second operation, the tree looks like this:

The distance between vertices 11 and 66 is 33, the distance between vertices 44 and 66 is 33, the distance between vertices 11 and 44 is 22. The positivity of this tree is equal to the minimum of these distances. It equals 22.

In the third testcase, after the fourth operation, the tree looks like this:

The positivity of this tree is 22.