CF1994A.Diverse Game

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Diverse Game

Petr, watching Sergey's stream, came up with a matrix aa, consisting of nn rows and mm columns (the number in the ii-th row and jj-th column is denoted as ai,ja_{i, j}), which contains all integers from 11 to nmn \cdot m. But he didn't like the arrangement of the numbers, and now he wants to come up with a new matrix bb, consisting of nn rows and mm columns, which will also contain all integers from 11 to nmn \cdot m, such that for any 1in,1jm1 \leq i \leq n, 1 \leq j \leq m it holds that ai,jbi,ja_{i, j} \ne b_{i, j}.

You are given the matrix aa, construct any matrix bb that meets Petr's requirements, or determine that it is impossible.

Hurry up! Otherwise, he will donate all his money to the stream in search of an answer to his question.

Input

Each test consists of multiple test cases. The first line contains an integer tt (1t1031 \leq t \leq 10^3) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains two integers nn and mm (1n,m101 \leq n, m \leq 10) — the number of rows and columns of matrix aa.

The next nn lines contain mm integers each, describing matrix aa. The ii-th of these lines contains the elements of matrix ai,1,ai,2,,ai,ma_{i, 1}, a_{i, 2}, \ldots, a_{i, m}.

It is guaranteed that all numbers in matrix aa are distinct and 1ai,jnm1 \leq a_{i, j} \leq n \cdot m.

It is guaranteed that the sum of nmn \cdot m over all test cases does not exceed 51045 \cdot 10^4.

Output

For each test case, output nmn \cdot m integers — any suitable matrix bb, or 1-1 if such a matrix does not exist.

Note

In the first test case, there is only one element in the matrix, so matrix bb is the only matrix and it does not fit.

In the second test case a1,1=21=b1,1a_{1, 1} = 2 \neq 1 = b_{1, 1}, a2,1=12=b2,1a_{2, 1} = 1 \neq 2 = b_{2, 1}.

Samples

5
1 1
1
2 1
2
1
1 5
2 4 5 3 1
2 4
1 2 3 4
5 6 7 8
3 3
4 2 1
9 8 3
6 7 5
-1
1 
2 
4 5 3 1 2 
6 7 8 5 
2 3 4 1 
8 3 9 
7 5 6 
2 1 4 

在线编程 IDE

建议全屏模式获得最佳体验