Once upon a time, there were medieval towns in the beautiful Moldavian territory, uniquely numbered from through . The town numbered with was the capital city. The towns were connected by bidirectional roads, each road having a length expressed in kilometers. There was a unique way to travel between any pair of towns without going through a town twice (i.e. the graph of roads was a tree).
When a town was attacked, the situation had to be reported as soon as possible to the capital. The message was carried by harbingers, one of which resided in each town. Each harbinger was characterized by the amount of time required to start the journey and by his constant speed (expressed in minutes per kilometer) after departure.
The message from a town was always carried on the unique shortest path to the capital. Initially, the harbinger from the attacked town carried the message. In each town that he traversed, a harbinger had two options: either go to the next town towards the capital, or leave the message to the harbinger from this town. The new harbinger applied the same algorithm as above. Overall, a message could be carried by any number of harbingers before arriving in the capital.
Your task is to find, for each town, the minimum time required to send a message from that town to the capital.
Constraints
The length of each road will not exceed .
For of the tests, .
For of the tests, each town will have at most adjacent roads (i.e., the graph of roads will be a line).
Input Specification
The first line contains one integer , the number of towns in Moldavia. Each of the following lines contains three integers , separated by one space, describing a road of length kilometers between towns numbered with and .
Subsequently, pairs of integers follow, one per line. The pair, , describes the characteristics of the harbinger in the town: is the number of minutes to prepare for the journey, and is the number of minutes needed to travel one kilometer. There is no harbinger in the capital.
Output Specification
The output should consist of exactly one line containing integers.
The number represents the minimum time, in minutes, required to send a message from the town to the capital.
Sample Input
5
1 2 20
2 3 12
2 4 1
4 5 3
26 9
1 10
500 2
2 30
Sample Output
206 321 542 328
Explanation for Sample
The roads and their lengths are shown in the image above. The start-up time and speed of the harbingers are written between brackets.
The minimum time to send a message from town to the capital is achieved as follows. The harbinger from town takes the message and leaves the town after minutes. He walks kilometers in minutes before arriving in town . There he leaves the message to the harbinger from that town. The second harbinger requires minutes to start the journey and walks for minutes before arriving to the capital.
The total time is therefore .
Comments