#P7359. 「JZOI-1」旅行

「JZOI-1」旅行

Description

This trip takes place in a country with NN cities and (N1)(N-1) bidirectional roads, and it is guaranteed that any two cities are reachable from each other.

To beautify the environment, all roads are built along rivers. This means Xiao Xi can make a boat by himself and then row along a road, so each time he traverses an edge, he may either walk on land or go by boat on the river.

Of course, because of downstream and upstream effects, there is a parameter ziz_i. In other words, if the time to walk across this edge on land is aia_i, then the time to row downstream is aizia_i-z_i (guaranteed to be greater than 00), and the time to row upstream is ai+zia_i+z_i. However, building a boat takes LL time, and once a person gets on land, they must abandon the boat.

Now Xiao Xi wants you to help compute the shortest time from uu to vv.

Note: A boat can be used continuously for multiple water segments (as long as you do not get off the boat).

Input Format

The first line contains three integers N,L,TN,L,T.

The next (N1)(N-1) lines each contain five integers xi,yi,ai,zi,typeix_i,y_i,a_i,z_i,type_i, where typei=1type_i=1 means the water flows from xix_i to yiy_i, and typei=0type_i=0 means the water flows from yiy_i to xix_i.

The next TT lines each contain two integers ui,viu_i,v_i, representing the start and end of each planned trip.

Output Format

Output TT lines, each containing one number, the answer for each query.

3 2 2
1 2 2 1 0
1 3 3 2 1
2 3
1 2
4
2
4 1 1
1 2 100 99 1
2 3 100 99 0
3 4 100 99 1
1 4
104

Hint

Explanation of Sample 1

The graph looks like this:

For the first query, from 22 to 33, we can build a boat at node 22, costing 22 time, then go downstream from node 22 to 11, costing 21=12-1=1, and then go downstream to 33, costing 32=13-2=1. So the total cost is 2+1+1=42+1+1=4.

Constraints

For 10%10\% of the testdata, N,T103N,T\leq10^3.

For another 10%10\% of the testdata, the shape of the tree is random.

For another 20%20\% of the testdata, all uu are the same, or all vv are the same.

The last test point provides a chain.

For 100%100\% of the testdata, N,T2×105N,T\leq2\times10^5, and 0<ai,L1050<a_i,L\leq10^5.

Translated by ChatGPT 5