3029: Annual Party

时间限制:1000 ms 内存限制:128 MB
上传者:
提交:1 通过:1

题目描述

There are $n$ cities in the Rainbow Island, connected by $n - 1$ bidirectional roads. In the $i^{th}$ of each next $m$ years, $k_{i}$ of the cities would like to hold a party and they would choose one from these $k_{i}$ cities as the place for the party. For convenience, every year, the sum of distance from the chosen city to all of these $k_{i}$ cities should be minimized.

Now you are given the map of the Rainbow Island and the $k_{i}$ cities which would hold the party, calculate the minimized sum of distance from the chosen city to all of the $k_{i}$ cities every year.

输入格式

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

For each test case :
The first line contains two integers $n$, $m$($1 \leq n , m\leq 100000$), number of cities and years.

The following $n - 1$ lines, each contains three integers $u, v, w$($1 \leq u, v \leq n$, $u \neq v$, $1 \leq w \leq 10^{9}$), which means there is a bidirectional road between $u$ and $v$, the length of road is $w$.

It's guaranteed that the graph is connected.

i-th of each next $m$ lines contains $k_{i} + 1$ integers, first is the number of cities for the party $k_{i}$($1 \leq k_{i} \leq n$), then come $k_{i}$ different integers $a_{1}$, $a_{2}$, ... , $a_{k_{i}}$($1 \leq a_{j} \leq n$, $1 \leq j \leq k_{i}$), the indexes of these $k_{i}$ cities.

It's also guaranteed that for each test case $\sum k_{i} \leq 100000$.

输出格式

For every year print the answer.

输入样例 复制

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

输出样例 复制

0
22