Under construction

Still being built. Things may change

Finding a Tree Connected Component

Time limit1sMemory limit1024 MB

Summary
Given a rooted tree and several vertex sets, report the vertex count and vertex-number sum of the smallest connected subtree containing each set.
Level

Medium6 of 10

Topics
Tree, Sorting
Solved
No attempts yet

Problem

A tree with NN vertices and N−1N-1 undirected edges is given. Vertices are numbered from 00 to N−1N-1, and vertex 00 is the root. Perform the following query QQ times.

  • kk v1v_1 v2v_2 ... vkv_k: Let CC be the connected component with the fewest vertices that contains all kk vertices v1,v2,...,vkv_1, v_2, ..., v_k. Print the number of vertices in CC and the sum of the vertex numbers in CC, separated by a space.

Input

The first line contains NN. (2≤N≤1000002 \le N \le 100000)

The next N−1N-1 lines each contain a parent vertex pp and a child vertex cc, separated by a space. The given graph is a tree. (0≤p≤N−10 \le p \le N-1, 0≤c≤N−10 \le c \le N-1, p≠cp \ne c)

The next line contains QQ. (1≤Q≤1000001 \le Q \le 100000)

The next QQ lines each contain k,v1,v2,...,vkk, v_1, v_2, ..., v_k, separated by spaces. The sum of all kk is at most 10610^6. (1≤k≤N1 \le k \le N, 0≤vi≤N−10 \le v_i \le N-1 (1≤i≤k1 \le i \le k), vi≠vjv_i \ne v_j (1≤i<j≤k1 \le i < j \le k))

All numbers in the input are integers.

Output

For each query, print the answer on its own line.

Examples1

  1. Example 1

    Input
    7
    0 1
    0 2
    1 3
    1 4
    2 5
    2 6
    5
    1 1
    2 0 1
    2 1 2
    3 2 3 4
    7 0 1 2 3 4 5 6
    
    Expected output
    1 1
    2 1
    3 3
    5 10
    7 21