//* Problem  : 
//* Contest  : acm.tju.edu.cn/toj
//* Type     : Solution
//* Date     : 2008.08.05
//* Author   : alt
//* Language : C++
//* Compiler : Microsoft Visual C++ 6.0

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

typedef pair <int,int> PIII;
typedef vector <int> VI;
typedef vector <vector<int> > VVI;
typedef vector <pair<int,int> > VPII;
typedef vector <vector<pair<int,int> > > VVPII;

#define int64 long long
#define PI (2.0 * acos(0.0))
#define MP make_pair
#define PB push_back
#define SZ(a) (int)a.size()
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORSZ(i, a) FOR(i, SZ(a))
#define FORE(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define FORR(i, a, b) for (int i = (int)(a); i >= int(b); i--)
#define INF 1000000000
#define INFL 1000000000000000000LL
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define CLEARA(a) memset(a, 0, sizeof(a))

inline int si(){int tt; scanf("%d", &tt); return tt;}

int n, m, res1, res2;

int a[128][1024];

VVI b;

int bb[1024*128][2], ib;

int c[128];

void solve(int k)
{
	res1 = m; res2 = 0;
	int j = 0;
	for (int i = m - 1; i >= 0; i--)
	{
		while (j < ib && (bb[j][1] == k || bb[j][0] < a[k][i])) j++;
		if (j < ib)
		{
			res1--; j++;
		}
	}
	memset(c, 0, sizeof(c));
	FOR(i, m)
	{
		FOR(j, n)
			if (j != k)
			{
				while (c[j] < m && a[j][c[j]] >= a[k][i]) c[j]++;
				if (c[j] >= m) return;
				c[j]++;
			}
		res2++;
	}
}

void result()
{
	printf("%d %d\n", res1, res2);
}

int cnt[301];

void _sort(int k)
{
	FOR(i, m)
		cnt[a[k][i]]++;
	int j = m;
	FOR(i, 301)
		while (cnt[i] > 0)
		{
			cnt[i]--;
			a[k][--j] = i;
		}
}

struct item{
	int val, p;
}list[1024*129];

int plist;

int root[301];

void _add(int v, int val)
{
	int p = ++plist;
	list[p].val = val;
	list[p].p = root[v];
	root[v] = p;
}

int main()
{
#ifdef _DEBUG
	freopen("1064", "r", stdin);
#endif
	int nt = si();
	while (nt--)
	{
		plist = 0;
		memset(root, 0, sizeof(root));
		n = si(); m = si();
		FOR(i, n)
		{
			FOR(j, m)
			{
				a[i][j] = si();
				_add(a[i][j], i);
			}
			_sort(i);
		}
		ib = 0;
		FOR(i, 301)
		{
			int p = root[i];
			while (p)
			{
				bb[ib][0] = i;
				bb[ib++][1] = list[p].val;
				p = list[p].p;
			}
		}
/*			FORSZ(j, b[i])
			{
				bb[ib][0] = i;
				bb[ib++][1] = b[i][j];
			}*/
		FOR(i, n)
		{
			solve(i);
			result();
		}
	}
	return 0;
}

