Quelling Blade
Time limit1sMemory limit128 MB
Given a tree of weapon prerequisites with costs and benefits, find a buying order that reaches the root in minimum time while maximizing the sum over time of owned benefit.
Problem

A hero is playing a game in which the strongest weapon is the Quelling Blade. Every type of weapon has a benefit value and a cost . Benefits add up: if the hero owns two weapons with benefits and , his current benefit is . Owning several weapons of the same type stacks their benefits as well.
To buy a weapon, the hero may first need other weapons. For example, if a weapon requires two Demon Edges, the hero must already own two Demon Edges before buying it; buying a second copy of that weapon requires two more Demon Edges. A required weapon is not consumed when the purchase is made, and one weapon type may require several weapons of the same type. Each weapon type is required by at most one other weapon type, so the dependencies form a tree rooted at the Quelling Blade.
The hero earns exactly coin per second and spends coins to buy weapons; a weapon of cost can be bought once the hero has accumulated unspent coins. Because he must pay for the whole tree of prerequisites, the minimum time to obtain the Quelling Blade equals the total cost of every weapon he has to buy, and this minimum is reached no matter which valid buying order he follows.
The hero is a perfectionist: among all buying orders that reach the Quelling Blade in this minimum time, he wants to maximize the utility, defined as the sum, over each second from the start of the game up to (but not including) the second in which he obtains the Quelling Blade, of the total benefit he currently owns. Equivalently, a weapon whose purchase completes at time contributes its benefit during each of the seconds that remain before the Quelling Blade is obtained, where is the total buying time.
Report the maximum possible utility.
Input
The first line contains the number of test cases . Most test cases are small.
For each test case, the first line contains a single integer , the number of weapon types .
The weapons are then described one by one. For weapon (weapons are numbered from to ):
- a line with two integers and , its benefit value and cost ;
- a line with a single integer , the number of distinct weapon types it requires;
- lines, each with two integers and , meaning this weapon requires weapons of type .
Weapon is the Quelling Blade. The total number of weapons that must be bought to obtain one Quelling Blade is less than , and each weapon type is required by at most one other weapon type.
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from and is the maximum utility. The answer always fits in a signed 64-bit integer.