Under construction

Still being built. Things may change

Spring cleaning

Time limit1sMemory limit128 MB

Summary
Given a tree and Q variations that attach extra leaves, find the minimum total length of leaf-pair paths that cover every edge, or -1 if impossible.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, Greedy
Solved
No attempts yet

Problem

This tree has NN nodes numbered from 1 to NN, connected by N−1N-1 edges. The edges have gathered too much dust, so Flóra's mother decided to clean them.

Cleaning the edges of an arbitrary tree is done by repeating the following process. Choose two different leaves (a node is a leaf if it is connected to exactly one other node by an edge), and clean every edge on the shortest path between them. If this path has dd edges, the cost of cleaning it is dd.

To avoid harming the leaves, each leaf may be chosen at most once. A tree is clean when all of its edges are cleaned. The cost is the sum of the costs of all cleaned paths.

Flóra thinks the tree is too small and simple, so she imagines QQ variations of it. In the ii-th variation, she adds a total of DiD_i extra leaves to the original tree. For each new leaf, she picks a node of the original tree and connects that node to the new leaf with an edge. Some nodes may stop being leaves after this step.

For each of the QQ variations, find the minimum cost required to clean the tree. Each variation starts from the original tree.

Input

The first line contains two integers NN and QQ.

Each of the next N−1N-1 lines contains two integers uu and vv, meaning that nodes uu and vv are connected by an edge.

Each of the next QQ lines describes one variation. The first integer on the ii-th line is DiD_i, followed by DiD_i integers aja_j. Each aja_j is the node to which a new leaf is attached. Several new leaves may be attached to the same node.

Output

Print QQ lines. On the ii-th line, print the minimum cost to clean the tree in the ii-th variation. If the tree cannot be cleaned, print −1-1.

Constraints

3≤N≤1053 \le N \le 10^5

1≤Q≤1051 \le Q \le 10^5

1≤u,v≤N1 \le u, v \le N

1≤Di≤1051 \le D_i \le 10^5 for all ii

∑Di≤105\sum D_i \le 10^5 for 1≤i≤Q1 \le i \le Q

1≤aj≤N1 \le a_j \le N for all jj in every variation

Hint

The picture shows the second variation.

One possible solution is to clean the paths between leaves 1 and 6, A and 7, and B and 3.

Examples1

  1. Example 1

    Input
    7 3
    1 2
    2 4
    4 5
    5 6
    5 7
    3 4
    1 4
    2 2 4
    1 1
    
    Expected output
    -1
    10
    8