#t505. 小明的账单

小明的账单

Xiao Ming's Bills

Problem Background

Special for beginners, ^_^

Problem Description

At a party, Xiao Ming accidentally lost his wallet. In the days that followed, he would face a series of card reissue procedures and a pile of bills...

After much pleading, the boss finally agreed to postpone the payment of the bills. But the boss also demanded that, each day, Xiao Ming must pick out the largest and the smallest unpaid bill and pay them off. The bills that remain unpaid are carried over to the next day.

Please help him work out the order of payments.

Input Format

The first line contains a positive integer NN (N≤15000N \le 15000), the total number of days Xiao Ming needs to reissue his UnionPay card.

Lines 22 to N+1N+1: each line describes the bills received that day. It starts with a non-negative integer M≤100M \le 100, the number of bills received that day, followed by MM positive integers (each less than 1,000,000,0001,000,000,000), the amounts of those bills.

The input guarantees that every day two bills can be paid.

Output Format

Output NN lines in total. Each line contains two integers separated by a space, representing the smallest and the largest bill amounts paid that day, respectively.

Sample Input

4
3 3 6 5
2 8 2
3 7 1 7
0

Sample Output

3 6
2 8
1 7
5 7

Constraints

Time limit: 1s; memory limit: 1024 KiB for each test case.