Time Limit: Java: 10000 ms / Others: 10000 ms

Memory Limit: Java: 32768 KB / Others: 32768 KB

A young farmer has N cows, but they produced really really a very very small amount of milk. John cannot live on the milk they made, so he's planning to eat some of the 'worst' cows to get rid of hunger. Each day, John chooses the cow that produces the LEAST amount of milk on that day and eat them. If there are more than one cow with minimal milk, John will be puzzled and will not eat any of them (Yeah! That's GREAT!!).

The i-th cow has a cycle of production Ti. That means, if it produces L unit milk on one day, it will also produce L unit after Ti days -- If it will not be eaten during these day :-). Though John is not a clever man, he doubts whether the cows will be eventually eaten up, so he asks for your help. Don't forget that he will offer you some nice beef for that!

The first line of the input contains a single integer T, indicating the number of test cases. (1<=T<=500) Each test case begins with an integer N(1<=N<=1000), the number of cows. In the following N lines, each line contains an integer Ti(1<=Ti<=10), indicating the cycle of the i-th cow, then Ti integers Mj(0<=Mj<=250) follow, indicating the amount of milk it can produce on the j-th day.

提交代码