LLM

Time limit: 0.65s Memory limit: 128MB Input: Output:

When people ask me, Flaviu, why I am such a die-hard fan of LucaLucaM, I often choose to quote a conversation I had with him nearly a year ago.
- Luca, why did you use HLD for this problem when it was just an Euler's Tour?
- Well, it was too boring with Euler's Tour, and I had two more hours in the contest, so I decided to use HLD at least.

Task

You are given a tree consisting of NN nodes and N1N - 1 bidirectional edges. You are also given a list of nodes SS, where each node in this list is of type BB. Each node of type BB is assigned a value. All other nodes are of type AA. You are given QQ queries. For each query, a set of nodes is provided, guaranteeing that all nodes in this set are of type AA. For each query, the nodes in the set have the signal initially. From then on, each second, any node of type AA will transmit the signal to all adjacent nodes, regardless of their type, and those of type BB will transmit the signal each second only to those adjacent with the same value or of type AA. This operation ends when there is no node left that does not have a signal and can receive it in the next second. You need to display for each query what is the maximum number of nodes that have the signal at the end.

Input data

The first line contains a natural number NN, the number of nodes.

Each of the following N1N-1 lines contains a pair of natural numbers aa and bb separated by spaces, indicating that there is an edge between nodes aa and bb.

The next line contains a natural number KK, the number of nodes of type BB.

The following KK lines contain pairs of two natural numbers, SiS_i and valival_i, indicating that node SiS_i is of type BB and has the value valival_i.

The following line contains a natural number QQ, the number of queries.

The following QQ lines contain a natural number cnticnt_i, representing the number of nodes that initially have the signal in query ii, followed by cnticnt_i numbers that represent the nodes that initially have the signal.

Output data

You will print the answers for each of the QQ queries, in order, each on a new line.

Constraints and clarifications

  • 1N,Q300 0001 \leq N, Q \leq 300 \ 000;
  • 1KN1 \leq K \leq N;
  • 0vali1 000 000 0000 \leq {val}_{i} \leq 1\ 000\ 000\ 000;
  • Let Mi|M_i| denote the cardinality of the set of nodes corresponding to query ii. Then, i=1QMi3 000 000\sum_{i=1}^{Q} |M_i| \leq 3\ 000\ 000.
  • Due to the very large input set, it is recommended to use fastio.
  • For test cases worth 1010 points: all nodes on even levels are of type AA;
  • For other test cases worth 2020 points: N,Q2 000N, Q \leq 2 \ 000;
  • For other test cases worth 3030 points: the tree is a chain of the form 12N1 - 2 - \dots - N.

Example 1

stdin

10
1 2
1 3
2 7
2 8
3 4
3 5
3 6
8 9
4 10
7
2 3
3 6
7 3
9 4
4 4
5 6
6 7
4
3 1 8 10
1 10
1 1
1 8

stdout

9
2
7
7

Explanation

We will color the nodes with signal in green. In the first query, the tree looks like this:

In the second query, the tree looks like this:

In the third and fourth queries, the tree looks like this:

Log in or sign up to be able to send submissions!