ANTS

Given a tree and a set of up to 50 marked nodes per query, find the node minimizing the sum of distances to all marked nodes, for up to 5000 queries.

Hard8TreeDFSPrefix sumLinked listNo attempts yetTime limit2sMemory limit512 MB

Problem

ANTS operates NN warehouses numbered 1 to NN. A special rail connects exactly two different warehouses. There are exactly N1N - 1 rails, and every warehouse can be reached from every other warehouse, so the rails connect the warehouses as a tree.

From time to time some robots need recalibration. The KK affected robots must all gather at a single warehouse, which can be any of the NN warehouses. Each robot travels along the shortest path, and every traversal of one rail counts once. The manager picks the gathering warehouse that makes the total count as small as possible.

As a small illustration, take a star where warehouses 2, 3, and 4 each connect to warehouse 1, with one robot at each of 2, 3, and 4. Gathering at warehouse 1 costs 1+1+1=31 + 1 + 1 = 3 rail uses, while gathering at warehouse 2 costs 0+2+2=40 + 2 + 2 = 4. The best gathering point is warehouse 1 with a total of 3.

There are QQ queries. Each query gives the locations of the KK affected robots, and for each query you must output the smallest achievable total.

Input

The first line holds the number of warehouses, 1N100,0001 \le N \le 100{,}000. Each of the following N1N - 1 lines holds two integers 1a,bN1 \le a, b \le N describing a rail between warehouses aa and bb. Every warehouse is reachable from every other warehouse. The next line holds the number of queries, 1Q50001 \le Q \le 5000. Each of the next QQ lines holds one query: an integer 1K501 \le K \le 50 followed by KK integers 1AiN1 \le A_i \le N giving the warehouses of the affected robots. Several robots can share one warehouse.

Output

For each query, output one line with the smallest achievable total number of rail uses.