Time Limit: 20000/10000 MS (Java/Others)

Memory Limit: 327680/327680 K (Java/Others)

Alice and Bob are playing "Gems Fight!":

There are Gems of G different colors , packed in B bags. Each bag has several Gems. G different colors are numbered from color 1 to color G.

Alice and Bob take turns to pick one bag and collect all the Gems inside. A bag cannot be picked twice. The Gems collected are stored in a shared cooker.

After a player ,we name it as X, put Gems into the cooker, if there are S Gems which are the same color in the cooker, they will be melted into one Magic Stone. This reaction will go on and more than one Magic Stone may be produced, until no S Gems of the same color remained in that cooker. Then X owns those new Magic Stones. When X gets one or more new Magic Stones, he/she will also get a bonus turn. If X gets Magic Stone in a bonus turn, he will get another bonus turn. In short,a player may get multiple bonus turns continuously.

There will be B turns in total. The goal of "Gems Fight!" is to get as more Magic Stones than the opponent as possible.

Now Alice gets the first turn, and she wants to know, if**both of them act the optimal way**, what will be the difference between the number of her Magic Stones and the number of Bob's Magic Stones at the end of the game.

There are Gems of G different colors , packed in B bags. Each bag has several Gems. G different colors are numbered from color 1 to color G.

Alice and Bob take turns to pick one bag and collect all the Gems inside. A bag cannot be picked twice. The Gems collected are stored in a shared cooker.

After a player ,we name it as X, put Gems into the cooker, if there are S Gems which are the same color in the cooker, they will be melted into one Magic Stone. This reaction will go on and more than one Magic Stone may be produced, until no S Gems of the same color remained in that cooker. Then X owns those new Magic Stones. When X gets one or more new Magic Stones, he/she will also get a bonus turn. If X gets Magic Stone in a bonus turn, he will get another bonus turn. In short,a player may get multiple bonus turns continuously.

There will be B turns in total. The goal of "Gems Fight!" is to get as more Magic Stones than the opponent as possible.

Now Alice gets the first turn, and she wants to know, if

There are several cases(<=20).

In each case, there are three integers at the first line: G, B, and S. Their meanings are mentioned above.

Then B lines follow. Each line describes a bag in the following format:

n c_{1} c_{2} ... c_{n}

It means that there are n Gems in the bag and their colors are color c_{1},color c_{2}...and color c_{n} respectively.

0<=B<=21, 0<=G<=8, 0<n<=10, S < 20.

There may be extra blank lines between cases. You can get more information from the sample input.

The input ends with G = 0, B = 0 and S = 0.

In each case, there are three integers at the first line: G, B, and S. Their meanings are mentioned above.

Then B lines follow. Each line describes a bag in the following format:

n c

It means that there are n Gems in the bag and their colors are color c

0<=B<=21, 0<=G<=8, 0<n<=10, S < 20.

There may be extra blank lines between cases. You can get more information from the sample input.

The input ends with G = 0, B = 0 and S = 0.

One line for each case: the amount of Alice's Magic stones minus the amount of Bob's Magic Stones.

提交代码