Contents

• # For Solution

In a certain video game, the player controls a hero characterized by a single integer value: power. The hero will have to beat monsters that are also characterized by a single integer value: armor.

On the current level, the hero is facing nn caves. To pass the level, the hero must enter all the caves in some order, each cave exactly once, and exit every cave safe and sound. When the hero enters cave ii, he will have to fight kiki monsters in a row: first a monster with armor ai,1ai,1, then a monster with armor ai,2ai,2 and so on, finally, a monster with armor ai,kiai,ki.

The hero can beat a monster if and only if the hero’s power is strictly greater than the monster’s armor. If the hero can’t beat the monster he’s fighting, the game ends and the player loses. Note that once the hero enters a cave, he can’t exit it before he fights all the monsters in it, strictly in the given order.

Each time the hero beats a monster, the hero’s power increases by 11.

Find the smallest possible power the hero must start the level with to be able to enter all the caves in some order and beat all the monsters.

Input Deep Down Below solution codeforces

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1051≤t≤105). Description of the test cases follows.

The first line of each test case contains a single integer nn (1n1051≤n≤105) — the number of caves.

The ii-th of the next nn lines contains an integer kiki (1ki1051≤ki≤105) — the number of monsters in the ii-th cave, followed by kiki integers ai,1,ai,2,,ai,kiai,1,ai,2,…,ai,ki (1ai,j1091≤ai,j≤109) — armor levels of the monsters in cave ii in order the hero has to fight them.

It is guaranteed that the sum of kiki over all test cases does not exceed 105105.

Output

For each test case print a single integer — the smallest possible power the hero must start the level with to be able to enter all the caves in some order and beat all the monsters.

Example
input

Copy Deep Down Below solution codeforces
2
1
1 42
2
3 10 15 8
2 12 11

output

Copy
43
13

Note Deep Down Below solution codeforces

In the first test case,  the hero has to beat a single monster with armor 4242, it’s enough to have power 4343 to achieve that.

In the second test case, the hero can pass the level with initial power 1313 as follows:

• enter cave 22:
• beat a monster with armor 1212, power increases to 1414;
• beat a monster with armor 1111, power increases to 1515;
• enter cave 11:
• beat a monster with armor 1010, power increases to 1616;
• beat a monster with armor 1515, power increases to 1717;
• beat a monster with armor 88, power increases to 1818.