Subtree Center Solution Codechef - You are given a tree with NN nodes (numbered 11 through NN) and QQ queries (numbered 11 through QQ). For each valid ii, in the ii-th query, you are given KiKi nodes

    

Subtree Center Solution Codechef -  

SOLUTION

” CLICK HERE “


You are given a tree with N nodes (numbered 1 through N) and Q queries (numbered 1 through Q). For each valid i, in the i-th query, you are given Ki nodes x1,x2,,xKi. Consider the smallest subtree which contains all of these nodes; you should find all centers of this subtree.

A node is called a center of a tree if it lies in the middle of at least one longest path in that tree. Note that there may be multiple longest paths (paths with the same maximum length) and for a longest path which contains an even number of nodes, there are two nodes lying in the middle of this path.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains two space-separated integers N and Q.
  • N1 lines follow. For each valid i, the i-th of these lines contains two space-separated integers u and v denoting that there is an edge between nodes u and v in the tree.
  • Q more lines follow. For each valid i, the i-th of these lines contains an integer Ki, followed by a space and Ki space-separated integers x1,x2,,xKi.

Note: The input and output of this problem are large, so prefer using fast input/output methods.

Output

For each query, print a single line containing an integer C denoting the number of centers, followed by a space and C space-separated integers — the nodes which are centers of the given subtree, sorted in increasing order.

Constraints

  • 1T105
  • 1N105
  • 1u,vN
  • the graph described on the input is a tree
  • 1Q106
  • 1xiN for each valid i
  • for each query, x1,x2,,xKi are pairwise distinct
  • the sum of N over all test cases does not exceed 5105
  • the sum of Ki over all queries in all test cases does not exceed 106

Example Input

2
3 2
1 2
2 3
2 1 3
2 1 2
5 2
1 2
2 3
2 4
3 5
1 2
2 4 5

Example Output

1 2
2 1 2
1 2
2 2 3

Explanation

Example case 1: The figure below describes the second query. The smallest subtree containing the given nodes is coloured red and the nodes which are its centers are coloured blue.

Example case 2: The figure below describes the second query with subtree and centers coloured in the same way.


Reactions