Given a tree with nodes, conveniently numbered from 1 to
, node
is the root node. You need to answer
queries. Each query is in the form
u v k
, which is where node
is on the path from
to
, i.e. adding up the
-th power of the depth of each node along the path from
to
.
Input Specification
The first line contains one integer, , (
), the number of nodes in the tree.
Each of the following lines contains two integers,
and
(
), an edge between
and
.
The next line contains one integer, , (
), the number of queries.
Each of the following lines contains three integers,
,
, and
, (
,
).
Output Specification
Print one integer for each query, the sum modulo .
Sample Input
5
1 2
1 3
2 4
2 5
2
1 4 5
5 4 3
Sample Output
33
17
Explanation
For the tree in the sample input, the depth of each node is: ,
,
,
,
.
For the first query, the path is 1-->2-->4
. Thus, the sum is .
For the second query, the path is 5-->2-->4
. Thus, the sum is .
Comments