#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
vector<pair<int, int> > v[200000];
int a[14][2005], vis[2005], pos[14], n, m, t, py[14], ans, mx[14];
void dfs(int d) {
  if (d == t) {
    for (int i = 1; i <= n; i++) mx[i] = 0;
    for (int j = 1; j <= t; j++)
      for (int i = 1; i <= n; i++)
        if (a[i][pos[j]] > mx[(i + py[j] + n - 1) % n + 1])
          mx[(i + py[j] + n - 1) % n + 1] = a[i][pos[j]];
    int s = 0;
    for (int i = 1; i <= n; i++) s += mx[i];
    ans = max(ans, s);
    return;
  }
  for (int i = 0; i < n; i++) {
    py[d + 1] = i;
    dfs(d + 1);
  }
}
int main() {
  int ncase;
  scanf("%d", &ncase);
  while (ncase--) {
    ans = 0;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        scanf("%d", &a[i][j]), v[a[i][j]].push_back({i, j});
    t = 0;
    for (int i = 100000; i >= 1; i--) {
      for (int j = (0); j < (v[i].size()); ++j)
        if (t < n && !vis[v[i][j].second]) {
          t++;
          vis[pos[t] = v[i][j].second] = true;
        }
      v[i].clear();
    }
    for (int i = 1; i <= t; i++) vis[pos[i]] = false;
    dfs(0);
    cout << ans << endl;
  }
  return 0;
}
