eolymp
bolt
Try our new interface for solving problems
Problems

Lumberjack villages

Lumberjack villages

Midgard has n villages connected by a network of roads. There are n - 1 roads in Midgard, and along these roads you can get from any village to any other. In other words, the road network forms a tree. Village number 1 is the capital. The Midgardians produce a lot of wood, from which they then build ships. Now the ruler of Midgard wants to build a large fleet in a year and set off to conquer new lands and resources. To do this, he decided to apply the following strategy:

Send governors to some villages. Moreover, on a simple path from the village to which the governor was sent to the capital, there should not be another village to which the governor was also sent. Please note that you can send one governor to the capital, but in this case you cannot send the governor to any other village.

The governor in the village v is given control over all the villages, from which the village v is located on a simple path to the capital. The village v is given also to the governor.

For each village, the value ai is known - the number of ships that this village will build in a year if it is not subordinate to any governor.

If there is a governor in the village v, then he acts as follows:

  • He selects a subset of W villages adjacent to v that are under his control. In these villages, he develops large workshops for the production of ships. For each village, it is known that if a workshop is built in it, then timber from bi other villages must be supplied to this village, and this workshop will produce ci ships in a year.
  • Now he must choose exactly Σ bu (uW) villages from the remaining villages under his control, which will supply wood to the workshops. To which particular workshop each of them will supply wood is not so important.
  • The village i where the workshop is built will build ci ships in a year.
  • A village that supplies wood will not build ships.
  • All other villages under his control will build as many ships in a year as if they were not under his control. That is, the i-th village will build ai ships in a year.

The ruler can send out any number of governors. Assuming that the governors operate optimally, determine the maximum number of ships that can be built by all villages in a year.

Входные данные

The first line contains one integer t (1t5000) - the number of tests. It is followed by t tests.

Each test starts with one integer n (1n5000) - the number of villages in Midgard.

The next n lines contain three integers ai, bi and ci (1ai, ci109, 1bin) - the number of ships that the village will build in a year, not being subordinate to the governor, the number of villages that must supply timber to that village if a workshop is built in it, and the number of ships that the village will build in a year if a workshop is built in it.

The following n - 1 lines contain descriptions of roads in Midgard. Each line contains two integers vi, ui - the numbers of the villages connected by the road. It is guaranteed that the road network forms a tree.

It is guaranteed that the sum of n in all tests does not exceed 5000.

Output

For each test, print one integer - the maximum number of ships that all villages can build in total in a year with the correct distribution of governors.

Time limit 2 seconds
Memory limit 128 MiB
Input example #1
3
3
1 1 10
1 1 9
5 1 11
1 2
2 3
4
2 4 9
2 2 6
1 4 7
4 4 8
1 2
2 3
1 4
7
3 1 10
2 4 6
3 2 8
2 3 4
1 1 9
2 1 6
1 2 4
1 2
2 3
3 4
2 5
5 6
5 7
Output example #1
14
10
22
Source 2018 Цикл Интернет-олимпиад для школьников, вторая командная олимпиада сезона, усложненная номинация, October 20, Problem I