A=lambda:[*map(int,input().split())]
for _ in[0]*A()[0]:
n,m=A();a=[A()for _ in[0]*n];*b,=map(sorted,a);i=j=0
for t in zip(a,b):i,j,*_=[i for i,x,y in zip(range(m),*t)if x^y]or(i,j)
for r in a:r[i],r[j]=r[j],r[i]
print(*((i+1,j+1),[-1])[a>b])
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int nmax = 2e5 + 7;
vector<vector<int>>a(nmax);
int n, m, s1, s2;
void swapp(int x,int y)
{
s1 = x, s2 = y;
for(int i=0; i<n; i++)
swap(a[i][x], a[i][y]);
}
int main()
{
int tc;
cin >> tc;
while(tc--)
{
int i,j,x,s = 0, ok = 1;
s1 = s2 = 0;
cin >> n >> m;
for(i=0; i<n; i++)
for(j=0; j<m; j++)
{
cin >> x;
a[i].push_back(x);
}
for(i=0; i<n; i++)
{
vector<int> v;
map<int,int>mp;
for(j=0; j<m; j++)
{
v.push_back(a[i][j]);
mp[a[i][j]] = j;
}
sort(v.begin(),v.end());
for(j=0; j<m; j++)
if(a[i][j] != v[j] && s == 0)
{
swapp(j, mp[v[j]]);
s++;
}
}
for(i=0; i<n; i++)
{
for(j=1; j<m; j++)
{
if(a[i][j] < a[i][j-1])
ok = 0;
}
}
if(ok == 1)
cout << s1 + 1 << " " << s2 + 1;
else
cout << -1;
cout << endl;
for(i=0;i<n;i++)
a[i].clear();
}
return 0;
}
33. Search in Rotated Sorted Array | 17. Letter Combinations of a Phone Number |
5. Longest Palindromic Substring | 3. Longest Substring Without Repeating Characters |
1312. Minimum Insertion Steps to Make a String Palindrome | 1092. Shortest Common Supersequence |
1044. Longest Duplicate Substring | 1032. Stream of Characters |
987. Vertical Order Traversal of a Binary Tree | 952. Largest Component Size by Common Factor |
212. Word Search II | 174. Dungeon Game |
127. Word Ladder | 123. Best Time to Buy and Sell Stock III |
85. Maximal Rectangle | 84. Largest Rectangle in Histogram |
60. Permutation Sequence | 42. Trapping Rain Water |
32. Longest Valid Parentheses | Cutting a material |
Bubble Sort | Number of triangles |
AND path in a binary tree | Factorial equations |
Removal of vertices | Happy segments |
Cyclic shifts | Zoos |
Build a graph | Almost correct bracket sequence |