CF BUDDY
← Problems·

1684C · Column Swapping

1400 · brute force, constructive algorithms, greedy

Problem: You are given a grid with nn rows and mm columns, where each cell has a positive integer written on it. Let's call a grid good, if in each row the sequence of numbers is sorted in a non-decreasing order. It means, that for each 1in1 \le i \le n and 2jm2 \le j \le m the following holds: ai,jai,j1a_{i,j} \ge a_{i, j-1}.

You have to to do the following operation exactly once: choose two columns with indexes ii and jj (not necessarily different), 1i,jm1 \le i, j \le m, and swap them.

You are asked to determine whether it is possible to make the grid good after the swap and, if it is, find the columns that need to be swapped.

Input Format: Each test contains multiple test cases. The first line contains the number of test cases tt (1t1001 \le t \le 100). Description of the test cases follows.

The first line of each test case contains two integers nn and mm (1n,m21051 \le n, m \le 2 \cdot 10^5) — the number of rows and columns respectively.

Each of the next nn rows contains mm integers, jj-th element of ii-th row is ai,ja_{i,j} (1ai,j1091 \le a_{i,j} \le 10^9) — the number written in the jj-th cell of the ii-th row.

It's guaranteed that the sum of nmn \cdot m over all test cases does not exceed 21052 \cdot 10^5.

Output Format: If after the swap it is impossible to get a good grid, output 1-1.

In the other case output 22 integers — the indices of the columns that should be swapped to get a good grid.

If there are multiple solutions, print any.

Note: In the first test case the grid is initially good, so we can, for example, swap the first column with itself.

In the second test case it is impossible to make the grid good.

In the third test case it is needed to swap the first and the second column, then the grid becomes good.

Sample Cases

Case 1

Input

5
2 3
1 2 3
1 1 1
2 2
4 1
2 3
2 2
2 1
1 1
2 3
6 2 1
5 4 3
2 1
1
2

Output

1 1
-1
1 2
1 3
1 1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback