#P1843D. Apple Tree
Apple Tree
No submission language available for this problem.
Description
Timofey has an apple tree growing in his garden; it is a rooted tree of vertices with the root in vertex (the vertices are numbered from to ). A tree is a connected graph without loops and multiple edges.
This tree is very unusual — it grows with its root upwards. However, it's quite normal for programmer's trees.
The apple tree is quite young, so only two apples will grow on it. Apples will grow in certain vertices (these vertices may be the same). After the apples grow, Timofey starts shaking the apple tree until the apples fall. Each time Timofey shakes the apple tree, the following happens to each of the apples:
Let the apple now be at vertex .
- If a vertex has a child, the apple moves to it (if there are several such vertices, the apple can move to any of them).
- Otherwise, the apple falls from the tree.
It can be shown that after a finite time, both apples will fall from the tree.
Timofey has assumptions in which vertices apples can grow. He assumes that apples can grow in vertices and , and wants to know the number of pairs of vertices (, ) from which apples can fall from the tree, where — the vertex from which an apple from vertex will fall, — the vertex from which an apple from vertex will fall. Help him do this.
The first line contains integer () — the number of test cases.
The first line of each test case contains integer () — the number of vertices in the tree.
Then there are lines describing the tree. In line there are two integers and (, ) — edge in tree.
The next line contains a single integer () — the number of Timofey's assumptions.
Each of the next lines contains two integers and () — the supposed vertices on which the apples will grow for the assumption .
It is guaranteed that the sum of does not exceed . Similarly, It is guaranteed that the sum of does not exceed .
For each Timofey's assumption output the number of ordered pairs of vertices from which apples can fall from the tree if the assumption is true on a separate line.
Input
The first line contains integer () — the number of test cases.
The first line of each test case contains integer () — the number of vertices in the tree.
Then there are lines describing the tree. In line there are two integers and (, ) — edge in tree.
The next line contains a single integer () — the number of Timofey's assumptions.
Each of the next lines contains two integers and () — the supposed vertices on which the apples will grow for the assumption .
It is guaranteed that the sum of does not exceed . Similarly, It is guaranteed that the sum of does not exceed .
Output
For each Timofey's assumption output the number of ordered pairs of vertices from which apples can fall from the tree if the assumption is true on a separate line.
Note
In the first example:
- For the first assumption, there are two possible pairs of vertices from which apples can fall from the tree: .
- For the second assumption there are also two pairs: .
- For the third assumption there is only one pair: .
- For the fourth assumption, there are pairs: .

For the second example, there are of possible pairs of vertices from which apples can fall: . For the second assumption, there is only one possible pair: . For the third assumption, there are two pairs: .