Find Fame in the Disc Arena

No attempts yetTime limit5sMemory limit128 MB

Problem

In Grid World, the Disc Arena is a place where programs play gladiator disc games against other programs. The arena hosts $k$ games. Winning a game earns you fame and makes you popular in Grid World; losing a game deletes you. Winning the $i$-th game earns fame $f_i$, which need not be positive.

To be allowed to play the $i$-th game, you must first have played and won every game in a fixed, known set $S_i$. The games in $S_i$ are the prerequisites of game $i$. The prerequisite relation has no cycles: it can never happen that game $a$ is required for game $b$, $b$ is required for $c$, and $c$ is required for $a$. The only reason to play a game with negative fame is that it is a prerequisite of a game with higher positive fame.

A set $C$ of games is feasible if, for every game $i \in C$, all of its prerequisites are also in $C$; that is, $i \in C$ implies $S_i \subseteq C$. The fame of $C$ is the sum of the fames of all games in $C$.

Your goal is to become as famous as possible. Given the fames and prerequisites of the games, find a feasible set of games with maximum total fame.

The figure below shows examples; arrows indicate prerequisites. In the first example, game 1 has games 2 and 3 as prerequisites, and game 3 has game 2 as a prerequisite. The maximum fame there is obtained by selecting ${1, 2, 3}$: game 3 must be selected even though its fame is negative, because it is a prerequisite of game 1. Selecting no games (the empty set) is always feasible and yields fame $0$, so the answer is never negative.

Input

The first line contains the number of test cases (at most $100$).

The first line of each test case contains $k$, the number of games ($k \le 1000$). The games are labeled $1$ through $k$. The next $k$ lines describe the games. The $(i+1)$-th line has the form

$$f_i \quad d_i \quad u_1 \quad u_2 \quad \cdots \quad u_{d_i}$$

where $f_i$ is the fame gained by winning game $i$, $d_i = |S_i|$ is the number of prerequisites, and $u_1, \dots, u_{d_i}$ are the members of $S_i$ (the prerequisite game numbers).

Within each test case, the total number of prerequisites over all games, $\sum_i |S_i|$, is at most $6000$.

Output

For each test case, print the maximum fame attainable with a feasible set of games. For the $x$-th test case, print exactly:

Case x: Maximum attainable fame = y

where $y$ is the maximum fame.