This page is still under construction.

Parts of this page are still being built. What you see may change.

HH Country

Time limit10sMemory limit512 MB

Summary
For each query set of tree vertices, output twice the sum of pairwise tree distances.
Level

Hard8 of 10

Topics
Tree, DFS, Prefix sum
Solved
No attempts yet

Problem

HH is the strongest country in competitive programming. It has nn cities numbered 1 to nn, and the cities are connected by roads. Between any two different cities there is exactly one path, so the cities and roads of HH form a tree.

HH runs a contest to split the budget of the Forward-looking Infrastructure Development Program. The contest has mm rounds, and round ii decides how budget ii is distributed. The distribution follows the result of a double round robin among the kik_i cities attached to that budget. For two different participating cities A and B, one game is played with A travelling to B, and one game is played with B travelling to A. A round therefore holds ki×(ki−1)k_i \times (k_i - 1) games in total.

To itemize the travel expenses, HH needs the total travelling distance between the participating cities of each round. The distance of one game is the number of roads on the only path from the away city to the home city. For every round, compute the sum of that distance over all games of the round.

Input

The first line contains an integer TT, the number of test cases.

The first line of each test case contains two integers nn and mm, the number of cities and the number of rounds. Each of the next n−1n - 1 lines contains two integers uu and vv, meaning there is a road between city uu and city vv. Each of the next mm lines begins with an integer kik_i, the number of cities taking part in round ii, followed on the same line by the labels ci,1,ci,2,…,ci,kic_{i,1}, c_{i,2}, \dots, c_{i,k_i} of those cities.

  • 1≤T≤1001 \le T \le 100
  • 2≤n≤1052 \le n \le 10^5
  • 1≤m≤1051 \le m \le 10^5
  • 1≤u,v,ci,j≤n1 \le u, v, c_{i,j} \le n
  • 2≤ki≤n2 \le k_i \le n
  • All ci,jc_{i,j} in one round are distinct.
  • ∑iki≤2×105\sum_i k_i \le 2 \times 10^5 within one test case.
  • The input file is not larger than 60MB.

Output

For each round, print the total travelling distance of that round on its own line, in the order the rounds are given.

Examples2

  1. Example 1

    Input
    2
    2 2
    1 2
    2 1 2
    2 2 1
    5 3
    1 2
    2 3
    2 4
    1 5
    2 3 5
    3 3 4 5
    5 1 2 3 4 5
    
    Expected output
    2
    2
    6
    16
    36
    
  2. Example 2

    Input
    1
    6 3
    1 2
    2 3
    3 4
    4 5
    5 6
    2 1 6
    3 2 3 4
    6 1 2 3 4 5 6
    
    Expected output
    10
    8
    70