Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daughter's marriage, reforms must be finished as soon as possible.

The kingdom currently consists of n cities. Cities are connected by n - 1 bidirectional road, such that one can get from any city to any other city. As the King had to save a lot, there is only one path between any two cities.

What is the point of the reform? The key ministries of the state should be relocated to distinct cities (we call such cities important). However, due to the fact that there is a high risk of an attack by barbarians it must be done carefully. The King has made several plans, each of which is described by a set of important cities, and now wonders what is the best plan.

Barbarians can capture some of the cities that are not important (the important ones will have enough protection for sure), after that the captured city becomes impassable. In particular, an interesting feature of the plan is the minimum number of cities that the barbarians need to capture in order to make all the important cities isolated, that is, from all important cities it would be impossible to reach any other important city.

Help the King to calculate this characteristic for each of his plan.


-----Input-----

The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cities in the kingdom.

Each of the next n - 1 lines contains two distinct integers u_{i}, v_{i} (1 ≤ u_{i}, v_{i} ≤ n) — the indices of the cities connected by the i-th road. It is guaranteed that you can get from any city to any other one moving only along the existing roads.

The next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of King's plans.

Each of the next q lines looks as follows: first goes number k_{i} — the number of important cities in the King's plan, (1 ≤ k_{i} ≤ n), then follow exactly k_{i} space-separated pairwise distinct numbers from 1 to n — the numbers of important cities in this plan.

The sum of all k_{i}'s does't exceed 100 000.


-----Output-----

For each plan print a single integer — the minimum number of cities that the barbarians need to capture, or print  - 1 if all the barbarians' attempts to isolate important cities will not be effective.


-----Examples-----
Input
4
1 3
2 3
4 3
4
2 1 2
3 2 3 4
3 1 2 4
4 1 2 3 4

Output
1
-1
1
-1

Input
7
1 2
2 3
3 4
1 5
5 6
5 7
1
4 2 4 6 7

Output
2



-----Note-----

In the first sample, in the first and the third King's plan barbarians can capture the city 3, and that will be enough. In the second and the fourth plans all their attempts will not be effective.

In the second sample the cities to capture are 3 and 5.

Source: codeforces | Difficulty: VERY_HARD
Tags: Graph algorithms

Graph traversal

Sorting

Tree algorithms

Dynamic programming

Divide and conquer


Solutions:
import sys
from collections import deque

def solve():
	sys.setrecursionlimit(10 ** 6)
	readline = sys.stdin.readline
	writelines = sys.stdout.writelines
	N = int(readline())
	G = [[] for i in range(N)]
	for i in range(N - 1):
		(u, v) = map(int, readline().split())
		G[u - 1].append(v - 1)
		G[v - 1].append(u - 1)
	S = []
	FS = [0] * N
	LS = [0] * N
	depth = [0] * N
	stk = [-1, 0]
	it = [0] * N
	while len(stk) > 1:
		v = stk[-1]
		i = it[v]
		if i == 0:
			FS[v] = len(S)
			depth[v] = len(stk)
		if i < len(G[v]) and G[v][i] == stk[-2]:
			it[v] += 1
			i += 1
		if i == len(G[v]):
			LS[v] = len(S)
			stk.pop()
		else:
			stk.append(G[v][i])
			it[v] += 1
		S.append(v)
	L = len(S)
	lg = [0] * (L + 1)
	for i in range(2, L + 1):
		lg[i] = lg[i >> 1] + 1
	st = [[L] * (L - (1 << i) + 1) for i in range(lg[L] + 1)]
	st[0][:] = S
	b = 1
	for i in range(lg[L]):
		st0 = st[i]
		st1 = st[i + 1]
		for j in range(L - (b << 1) + 1):
			st1[j] = st0[j] if depth[st0[j]] <= depth[st0[j + b]] else st0[j + b]
		b <<= 1
	INF = 10 ** 18
	ans = []
	Q = int(readline())
	G0 = [[]] * N
	P = [0] * N
	deg = [0] * N
	KS = [0] * N
	A = [0] * N
	B = [0] * N
	for t in range(Q):
		(k, *vs) = map(int, readline().split())
		for i in range(k):
			vs[i] -= 1
			KS[vs[i]] = 1
		vs.sort(key=FS.__getitem__)
		for i in range(k - 1):
			x = FS[vs[i]]
			y = FS[vs[i + 1]]
			l = lg[y - x + 1]
			w = st[l][x] if depth[st[l][x]] <= depth[st[l][y - (1 << l) + 1]] else st[l][y - (1 << l) + 1]
			vs.append(w)
		vs.sort(key=FS.__getitem__)
		stk = []
		prv = -1
		for v in vs:
			if v == prv:
				continue
			while stk and LS[stk[-1]] < FS[v]:
				stk.pop()
			if stk:
				G0[stk[-1]].append(v)
			G0[v] = []
			it[v] = 0
			stk.append(v)
			prv = v
		que = deque()
		prv = -1
		P[vs[0]] = -1
		for v in vs:
			if v == prv:
				continue
			for w in G0[v]:
				P[w] = v
			deg[v] = len(G0[v])
			if deg[v] == 0:
				que.append(v)
			prv = v
		while que:
			v = que.popleft()
			if KS[v]:
				a = 0
				for w in G0[v]:
					ra = A[w]
					rb = B[w]
					if depth[v] + 1 < depth[w]:
						a += min(ra, rb + 1)
					else:
						a += ra
				A[v] = INF
				B[v] = a
			else:
				a = 0
				b = c = INF
				for w in G0[v]:
					ra = A[w]
					rb = B[w]
					(a, b, c) = (a + ra, min(a + rb, b + ra), min(b + rb, c + min(ra, rb)))
				A[v] = min(a, b + 1, c + 1)
				B[v] = b
			p = P[v]
			if p != -1:
				deg[p] -= 1
				if deg[p] == 0:
					que.append(p)
		v = min(A[vs[0]], B[vs[0]])
		if v >= INF:
			ans.append('-1\n')
		else:
			ans.append('%d\n' % v)
		for v in vs:
			KS[v] = 0
	writelines(ans)
solve()