PivotOJ

Mreža

시간 제한: 5000ms메모리 제한: 1024MB출처: COCI 2022-2023BOJ 27550

문제

The city mayor Mirko lives in a city with nn neighborhoods connected with n1n - 1 bidirectional roads such that from any neighborhood it is possible to reach every other neighborhood.

Mirko wants to upgrade some roads to reduce traffic. For every road, we know the current speed viv_i vehicles drive on it, the price of upgrading cic_i and the speed of driving after upgrading sis_i.

There are qq unsatisfied citizens that come to visit Mirko. Each one has their suggestion for an upgrade. The suggestion of the ii-th citizen is: “We should invest eie_i euros in upgrading roads between neighborhoods aia_i and bib_i

For each suggestion, Mirko is interested in what is the minimum driving speed between neighborhoods aia_i and bib_i if he spends at most eie_i euros on upgrading the roads, given thathis goal is to maximize the minimum driving speed between the neighborhoods aia_i and bib_i.

Mirko soon realized that calculating this is not an easy task and hired you to help him!

입력

The first line contains the integer nn (2 ≤ n ≤ 100\,000), the number of neighborhoods.

In each of the next n1n - 1 lines there are five integers xix_i, yiy_i, viv_i, cic_i, sis_i (1 &le; x_i , y_i &le; n, 1 &le; v_i < s_i &le; 10^9, 1 &le; c_i &le; 10^9), denoting that neighborhood xix_i and yiy_i are connected, current driving speed is viv_i, cost of upgrading the road is cic_i, and the speed on the road would be sis_i.

The next line contains the integer qq (1 &le; q &le; 100\,000), the number of unsatisfied citizens.

In each of the next qq lines there are three integers aia_i, bib_i, eie_i (1 &le; a_i , b_i &le; n, aibia_i \ne b_i, 1 &le; e_i &le; 10^{18}), which describe the suggestion of the ii-th citizen.

출력

In the ii-th of the qq lines print the answer to the request of the ii-th citizen.

힌트

Clarification of the first example:

The illustration represents the city and its neighborhoods. On the edges are written the current driving speed, the cost of upgrading, and the speed after upgrading, respectively.

If we upgrade the roads between 11 and 22, and between 11 and 33, the driving speeds from 22 to 44 will be 1010, 99, and 77 m/s. The minimum is 77 m/s.

If we upgrade the roads between 44 and 33, the driving speeds from 66 to 44 will be 55 an 1515 m/s. The minimum is 55 m/s.

If we upgrade the road between 33 and 55, the driving speed from 55 to 33 will be 1111 m/s.

예제

예제 1

입력
6
1 2 5 7 10
1 3 4 8 9
3 4 7 1 15
3 5 6 3 11
3 6 5 6 8
3
2 4 15
6 4 5
3 5 10
출력
7
5
11

예제 2

입력
4
1 2 5 5 8
2 3 4 6 9
3 4 6 10 7
4
1 4 16
2 4 16
1 4 10
3 4 10
출력
6
7
5
7
코드를 제출하려면 로그인하세요.