#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
template < typename Tp >
void read(Tp &x) {
x = 0; int fh = 1; char ch = 1;
while(ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if(ch == '-') fh = -1, ch = getchar();
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
x *= fh;
}
const int MAXN = 50000 + 7;
const int MAXM = 10 + 3;
int n, m;
struct Info{
int a[MAXM];
Info(){
a[0] = a[1] = a[2] = a[3] = a[4] = a[5] = a[6] = a[7] = a[8] = a[9] = a[10] = a[11] = a[12] = 0;
}
bool operator < (const Info &p) const {
for(int i = 1; i < m; i++) {
if(a[i] != p.a[i]) return a[i] < p.a[i];
}
return a[m] < p.a[m];
}
}p[MAXN];
vector <Info> vec;
void Init(void) {
read(n); read(m);
for(int i = 1; i <= n; i++) {
Info rev;
for(int j = 1; j <= m; j++) {
read(p[i].a[j]);
rev.a[p[i].a[j]] = j;
}
vec.push_back(rev);
}
}
int getans(Info ord, Info to) {
for(int i = 1; i <= m; i++) {
if(ord.a[i] != to.a[i]) return i - 1;
}
return m;
}
void Work(void) {
sort(vec.begin(), vec.end());
for(int i = 1; i <= n; i++) {
auto it = lower_bound(vec.begin(), vec.end(), p[i]);
int ans = 0;
if(it == vec.end()) {
--it;
ans = getans(p[i], *it);
}
else {
ans = getans(p[i], *it);
--it;
if(it != vec.end()) {
ans = max(ans, getans(p[i], *it));
}
}
printf("%d%c", ans, " \n"[i == n]);
}
}
signed main(void) {
int T; read(T);
while(T--) {
Init();
Work();
}
return 0;
}
代码时间复杂度应该是 O(nmlogn),但 TLE #2,不是很懂。