#CF2237H. 史莱姆与询问 / H. Slime and Queries
史莱姆与询问 / H. Slime and Queries
H. Slime and Queries
Source: Codeforces 2237H
Contest: Order Capital Round 2 (Codeforces Round 1104, Div. 1 + Div. 2)
Time limit: 5 seconds
Memory limit: 1024 megabytes
Statement
You are given a tree with vertices numbered from to . A slime occupies exactly vertices of the tree. It is guaranteed that the subgraph induced by the occupied vertices is connected.
Initially, the slime occupies vertices .
First, we define a function on a sequence of vertices. Consider a sequence . There are pieces of food. For each , the -th piece of food is located at vertex . At first, only the first piece of food appears.
The slime may perform the following operations any number of times:
- Move. The slime removes itself from one currently occupied vertex and expands to one currently unoccupied vertex. Formally, let be the current set of occupied vertices. Choose a vertex and a vertex , and replace with . After the operation, the subgraph induced by must still be connected.
- Eat. If the -th piece of food has appeared and the slime currently occupies vertex , then the slime may eat the -th piece of food. If , the -th piece of food appears immediately after that. Eating does not change the occupied vertices.
Define as the minimum number of Move operations needed for the slime to eat all pieces of food in order, starting from the initial occupied vertices .
There are queries. The input is forced online. The input gives encoded values . Let . For each , the actual vertex of the -th query is , where .
For each , output .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains three integers , , and (, ) — the number of vertices in the tree, the number of vertices occupied by the slime, and the number of queries.
Each of the next lines contains two integers and (, ), denoting an edge of the tree.
The next line contains distinct integers () — the vertices initially occupied by the slime. It is guaranteed that these vertices induce a connected subgraph.
The next line contains integers () — the encoded query vertices.
It is guaranteed that the sum of over all test cases does not exceed .
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output integers. The -th integer should be .
Note
In the explanations below, the underlined vertex is the newly occupied vertex after a Move, and vertices where the slime eats food are written in bold.
In the first test case, after decoding, the food appears at vertices in order. Initially, the slime occupies .
- For , the slime already occupies vertex , so .
- For , one optimal process is $[\mathbf{1},2]\to[2,\underline{3}]\to[3,\underline{\mathbf{4}}]$, so .
- For , one optimal process is $[\mathbf{1},2]\to[2,\underline{3}]\to[3,\underline{\mathbf{4}}]\to[3,\underline{\mathbf{5}}]$, so .
In the second test case, after decoding, the food appears at vertices in order. Initially, the slime occupies .
- For , one optimal process is , so .
- For , the same process also lets the slime eat at vertex , so .
- For , one optimal process is $[1,2,3]\to[1,3,\underline{\mathbf{5}}]\to[1,3,\underline{\mathbf{6}}]$, so .
- For , one optimal process is $[1,2,3]\to[1,3,\underline{\mathbf{5}}]\to[1,3,\underline{\mathbf{6}}]\to[1,\underline{\mathbf{2}},3]$, so .
In the third test case, after decoding, the food appears at vertices in order. Initially, the slime occupies .
- For , one optimal process is $[1,2,4]\to[1,2,\underline{3}]\to[1,3,\underline{\mathbf{7}}]$, so .
- For , one optimal process is $[1,2,4]\to[1,2,\underline{3}]\to[1,3,\underline{\mathbf{7}}]\to[1,\underline{2},3]\to[1,2,\underline{\mathbf{5}}]$, so .
- For , one optimal process is $[1,2,4]\to[1,2,\underline{3}]\to[1,3,\underline{\mathbf{7}}]\to[1,\underline{2},3]\to[1,2,\underline{\mathbf{5}}]\to[1,2,\underline{3}]\to[1,3,\underline{\mathbf{6}}]$, so .
- For , continue with $[1,3,\mathbf{6}]\to[1,\underline{2},3]\to[1,2,\underline{\mathbf{4}}]$, so .
- For , continue with , so .
In the fourth test case, after decoding, the food appears at vertices .
In the fifth test case, after decoding, the food appears at vertices .
In the sixth test case, after decoding, the food appears at vertices .
Examples
10
5 2 3
3 5
2 1
4 3
3 2
1 2
1 4 3
6 3 4
5 1
1 3
6 1
4 1
2 1
1 2 3
5 2 5 6
7 3 5
3 7
4 2
1 3
2 1
6 3
5 2
1 2 4
7 3 2 5 2
5 2 5
3 1
1 5
2 1
4 1
1 2
3 3 3 4 2
6 3 6
4 6
3 2
1 2
5 4
2 4
2 4 5
6 6 1 2 4 6
7 4 5
5 2
3 1
2 1
3 7
6 3
4 2
1 2 3 4
7 4 4 5 1
4 3 4
3 1
1 4
2 1
1 2 3
4 1 2 3
6 2 5
2 4
5 4
2 1
6 4
3 2
1 2
6 1 1 1 2
7 2 5
2 4
7 3
3 6
1 3
1 2
5 2
1 2
4 6 1 6 5
8 4 6
5 2
3 2
7 5
4 3
8 7
1 2
6 5
2 3 4 5
8 7 3 3 7 8
0 2 3
1 1 2 3
2 4 6 8 9
1 2 3 4 4
1 2 3 4 4 4
1 2 3 3 4
1 1 2 2
2 4 6 8 9
1 4 7 10 11
2 3 4 4 5 5