Limits 1s, 1.0 GB

Benjamin Gates the treasure hunter opened a coaching center named Khajna Dibo Na where he teaches his students to learn about treasure hunting. The coaching center is conducted by Benjamin Gates, Riley Pool, Abigail Chase, Patrick Gates and Emily Appleton.

Hasan Abdullah is a student of this coaching center. As he has a great interest in treasure hunting, the Khajna Dibo Na team proposed him to solve an assignment of treasure hunting in order to examine his excellence. They gave Hasan a map which is a treasure map. To gain all the treasures, Hasan needs to answer several queries.

The treasure map consists of several places. All the places are connected by some paths. It’s possible to visit between any two places by these paths. Each path between any two places contains an amount of treasure with weight CC. There is only one path to go from a single place to another place. Now, Hasan wants to gain all the treasures and he is ready to reply all the queries.

In each query, he will be given any two places suppose AA and BB. It might be possible to travel from place AA to place BB by crossing more than one places. As each of the places is connected by some paths he will have some treasures in each of the paths of several weights. While traveling, the authority tells him to collect all the treasures and suggests him to convert all the treasure weight into a magic weight. The magic weight is said to be the minimum weight among the treasures he collected. Hasan has to find out what will be the minimum amount of treasures he needs to reduce in order to convert all the treasure weight into the magic weight.

For example, if he has 3 edges of treasure value 3, 2, 5 between two places AA and BB. As the minimum treasure value is 2 in the 2nd edge so the magic weight should be 2. Hasan needs to convert the treasure value of all the edges into 2. And hence he has the minimum amount is 32+22+52=1+0+3=4|3 - 2| + |2 - 2| + |5 - 2| = 1 + 0 + 3 = 4.

Input

Input starts with an integers NN (1N5×1031 \le N \le 5 \times 10^3) where N denotes the number of places on the map.

Now, there are N1N-1 lines contains three integers uu, vv (1u,vN1 \le u, v \le N) and CC (1C2×1051 \le C \le 2 \times 10^5) that denotes a path between node uu and vv and the treasure value CC of between those two places.

The following line contains an integer QQ (0Q1050 \le Q \le 10^5) which denotes the number of queries is going to be asked.

The following QQ lines contain two integers AA and BB (1A,BN1 \le A, B \le N), denoting for which two places Hasan need to answer the question as stated above.

Output

For each Q line you need to print the required answer of the question i.e. the minimum amount of treasures he needs to reduce in order to convert the treasure weight of all the paths between two places into the magic weight.

Sample

InputOutput
3
1 2 3
2 3 2
2
1 2
1 3
0
1

For query 1, we have only 1 path and hence the amount we need is 33=03 – 3 = 0. For query 2, we have 2 paths i.e. 1-2, 2-3 and the minimum coin value is 2. So, the amount we need is (32)+(22)=1(3-2) + (2 -2) = 1.


Submit

Login to submit.

Statistics

70% Solution Ratio
as_coupleEarliest, Sep '17
Liad_HossainFastest, 0.0s
mihassanLightest, 1.6 MB
mihassanShortest, 1223B
Toph uses cookies. By continuing you agree to our Cookie Policy.