#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n, m, op[N], ans;
struct node {
int v, a[4];
}a[N];
bool cmp (node a, node b) {
return a.v > b.v;
}
int b[4];
void dfs (int p) {
if (p > n*2 || p > m) {
memset(b, 0, sizeof b);
for (int i = 1;i < p;i++) {
for (int j = 0;j < n;j++) b[j] = max(b[j], a[i].a[(j+op[i])%n]);
}
ans = max(ans, b[0]+b[1]+b[2]+b[3]);
return;
}
for (int i = 0;i < 4;i++) op[p] = i, dfs(p+1);
}
void solve () {
cin >> n >> m; ans = 0;
for (int i = 0;i < n;i++) for (int j = 1;j <= m;j++) {
cin >> a[j].a[i]; a[j].v = max(a[j].v, a[j].a[i]);
}
sort(a+1, a+1+m, cmp);
dfs(1);
cout << ans << '\n';
}
int main () {
freopen("happygameT1.in", "r", stdin);
freopen("happygameT1.out", "w", stdout);
int T = 1; cin >> T;
while (T--) solve();
return 0;
}