The only difference with E1 is the question of the problem.

Vlad built a maze out of $n$ rooms and $n-1$ bidirectional corridors. From any room $u$ any other room $v$ can be reached through a sequence of corridors. Thus, the room system forms an undirected tree.

Vlad invited $k$ friends to play a game with them.

Vlad starts the game in the room $1$ and wins if he reaches a room other than $1$, into which exactly one corridor leads. Friends are placed in the maze: the friend with number $i$ is in the room $x_i$, and no two friends are in the same room (that is, $x_i \neq x_j$ for all $i \neq j$). Friends win if one of them meets Vlad in any room or corridor before he wins.

For one unit of time, each participant of the game can go through one corridor. All participants move at the same time. Participants may not move. Each room can fit all participants at the same time.

Friends know the plan of a maze and intend to win. They don't want to waste too much energy. They ask you to determine if they can win and if they can, what minimum number of friends must remain in the maze so that they can always catch Vlad.

In other words, you need to determine the size of the minimum (by the number of elements) subset of friends who can catch Vlad or say that such a subset does not exist.


-----Input-----

The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. The input contains an empty string before each test case.

The first line of the test case contains two numbers $n$ and $k$ ($1 \le k < n \le 2\cdot 10^5$) — the number of rooms and friends, respectively.

The next line of the test case contains $k$ integers $x_1, x_2, \dots, x_k$ ($2 \le x_i \le n$) — numbers of rooms with friends. All $x_i$ are different.

The next $n-1$ lines contain descriptions of the corridors, two numbers per line $v_j$ and $u_j$ ($1 \le u_j, v_j \le n$) — numbers of rooms that connect the $j$ corridor. All corridors are bidirectional. From any room, you can go to any other by moving along the corridors.

It is guaranteed that the sum of the values $n$ over all test cases in the test is not greater than $2\cdot10^5$.


-----Output-----

Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be $-1$ if Vlad wins anyway and a minimal number of friends otherwise.


-----Examples-----

Input
4

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

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

3 1
2
1 2
2 3

3 2
2 3
3 1
1 2
Output
-1
2
1
2


-----Note-----

In the first set of inputs, even if all the friends stay in the maze, Vlad can still win. Therefore, the answer is "-1".

In the second set of inputs it is enough to leave friends from rooms $6$ and $7$. Then Vlad will not be able to win. The answer is "2".

In the third and fourth sets of inputs Vlad cannot win only if all his friends stay in the maze. Therefore the answers are "1" and "2".

Difficulty: HARD
Tags: Graph traversal

Shortest paths

Greedy algorithms

Tree algorithms

Dynamic programming
Skill types: Dynamic programming

Greedy algorithms

Starter code:

Solutions:
def solve():
	input()
	(n, k) = map(int, input().split())
	f = list(enumerate(list(map(lambda x: int(x) - 1, input().split()))))
	g = {i: [] for i in range(n)}
	for i in range(n - 1):
		(a, b) = map(int, input().split())
		a -= 1
		b -= 1
		g[a].append(b)
		g[b].append(a)
	needed = {}
	vlad = [0]
	visited = [False] * n
	friends = [-1] * n
	while len(vlad):
		nf = []
		for (i, f1) in f:
			if friends[f1] != -1:
				continue
			friends[f1] = i
			for e in g[f1]:
				nf.append((i, e))
		f = nf
		new_vlad = []
		for v in vlad:
			if friends[v] != -1:
				needed[friends[v]] = True
				continue
			if visited[v] == True:
				continue
			visited[v] = True
			if len(g[v]) == 1 and v != 0:
				return -1
			for e in g[v]:
				new_vlad.append(e)
		vlad = new_vlad
	return len(needed)
n = int(input())
for i in range(n):
	print(solve())


t = int(input())
for i in range(t):
	a = input()
	(n, k) = map(int, input().split())
	x = list(map(int, input().split()))
	for i in range(k):
		x[i] -= 1
	M = [list() for i in range(n)]
	for i in range(n - 1):
		(p, q) = map(int, input().split())
		M[p - 1].append(q - 1)
		M[q - 1].append(p - 1)
	F = [-1] * n
	(S, d) = (set(x), 0)
	while len(S) > 0:
		T = set()
		for j in S:
			F[j] = d
		for j in S:
			for q in M[j]:
				if F[q] == -1:
					T.add(q)
		S = T | set()
		d += 1
	V = [-1] * n
	(S, d, ret) = (set([0]), 0, 0)
	while len(S) > 0:
		T = set()
		for j in S:
			V[j] = d
		for j in S:
			for q in M[j]:
				if V[q] == -1:
					if F[q] > d + 1:
						T.add(q)
					else:
						ret += 1
		S = T | set()
		d += 1
	M1 = []
	for i in range(1, n):
		if len(M[i]) == 1:
			M1.append(i)
	bol = False
	for i in M1:
		if F[i] > V[i] and V[i] != -1:
			bol = True
			break
	if bol:
		print(-1)
	else:
		print(ret)