Online Shopping

Time limit1sMemory limit128 MB

Summary
Reorder rows and columns of a matrix so the resulting row-by-row string of prices is lexicographically smallest.
Level

Medium7 of 10

Topics
Brute force, Sorting, Greedy, Implementation
Solved
No attempts yet

Problem

As online shopping grows, services that let you compare the prices of many stores at a glance become popular. To show the cheapest offer for a product quickly, such a service wants to lay out its price table as tidily as possible.

You are given one price table. It has one row per product, for a total of aa rows, and one column per online shop, for a total of bb columns. The cell in row ii and column jj is the price of product ii at shop jj.

You may reorder the products (the rows) and the shops (the columns) independently and in any way you like. Once an arrangement is fixed, its table string is obtained by reading the table row by row: for each product in order, list that product's prices at every shop in order, separating all values by single spaces.

Among all arrangements of the rows and columns, output the one whose table string is smallest. Two table strings are compared value by value: at the first position where they differ, the one with the smaller price (an integer number of cents) is the smaller table string.

Input

The first line contains a single integer nn, the number of test cases.

Each of the next nn lines describes one test case. A line begins with two integers aa and bb (1≤a,b≤51 \le a, b \le 5), the number of products and the number of shops. They are followed by the a⋅ba \cdot b prices of the table's initial arrangement, given as its table string: for each product in order, that product's bb prices, one per shop. Every price pp is an integer number of cents with 0≤p≤1090 \le p \le 10^9.

Output

For each test case, print a line Scenario #i:, where ii is the test case number starting from 11. On the next line, print the table string of the optimal reordering of the products and shops. Separate two consecutive test cases with a blank line.

Examples1

  1. Example 1

    Input
    2
    3 2 3999 5000 4000 4000 12999 9999
    4 3 120 120 110 120 80 75 250 50 200 55 80 80
    
    Expected output
    Scenario #1:
    3999 5000 4000 4000 12999 9999
    
    Scenario #2:
    50 200 250 80 75 120 80 80 55 120 110 120