#OLD389. Melo's Mega-company
Melo's Mega-company
Description
Melo earned a lot of money by moving bricks and became the boss eventually, so he established a brick-moving mega-company, and so many people want to follow Melo and he decided to put all offices on a huge tree. There are nodes on this tree, representing N departments' offcie, and node 1 is the root of the tree, and it also is Melo's headquarters. Every node has edges that connected other node. Melo likes to interview some departments and then have a meeting with all the employees of the department. Due to the boss is generous, costs can be free if distance less than , otherwise they will cost the same as the distance.
When Melo arrives at node , he will ask all the employees of the department to gather at . Melo will ask the secretary to count all their travel expenses. He is not good at math, so can you help him figure out the answer?
Format
Input
The first line contains an integer , indicating that the tree has nodes.
The next lines describe the edges which connected different nodes,there are three integers denote that the distance between and is .
The line contains a positve integer , indicating the times of Melo's query.
For the next lines, each line contains two integers and .
,
Output
For each query, print a singe line containing one number, denoting the sum of all the nodes in the subtree of whose distance from is greater than or equal to .(If no such nodes exists, the answer is 0)
Samples
3
1 2 2
1 3 1
2
1 1
1 2
3
2