COCI 2009/2010, Contest #2

Task PASIJANS

"Pasijans, patience, or solitaire is the name for a group of single player card games. One new such game, so new it has no name, is played with cards sporting random integers as values. The game starts by shuffling all cards and distributing them in N sequences, not necessarily of equal length.

During each turn, the player can remove the first card in any sequence and place it at the end of the "Solution sequence". The card that was second in the selected sequence now becomes the first and the turn ends. Of course once the card is in the "Solution sequence" it cannot be removed, replaced or altered in any way. So don't even try.

The game ends when all cards are in the "Solution sequence". The object of the game is to construct the best possible "Solution sequence". One sequence is better than the other if for the first cards they differ, lets call them X and Y, the value on the card X is smaller than the value on the card Y.

Write a program that finds the best possible "Solution sequence".

Input

The first line contains one integer N (1 ≤ N ≤ 1000), the length of the initial sequence

The next N lines contain descriptions of input sequences. Each line starts with an integer L (1 ≤ L ≤ 1000), length of the sequence. It's followed by L integers, smaller than 100 000 000.

Output

One line containing $\textstyle \sum L$ numbers, the best possible "Solution sequence" obtainable.

Sample Tests

Input

3
1 2
1 100
1 1

Output

1 2 100

Input

2
5 10 20 30 40 50
2 28 27

Output

10 20 28 27 30 40 50

Input

2
3 5 1 2
3 5 1 1

Output

5 1 1 5 1 2

All Submissions
Best Solutions


Point Value: 20 (partial)
Time Limit: 6.00s
Memory Limit: 128M
Added: Aug 13, 2013

Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

Comments (Search)

Total time is O(NL * log N * log L)