#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, ans, crt[2005], t, p[2005];
vector<int> v[2005];
int uz[13], mx[13][13];
void _try() {
  int s = 0;
  for (int i = 1; i <= n; i++) s += mx[n][i];
  ans = max(ans, s);
}
void mark(int i, int off, int val) {
  for (int j = 1; j <= n; j++) {
    int y = j + off;
    if (y > n) y -= n;
    mx[i][y] = max(mx[i - 1][y], v[i][j]);
    if (v[i][j] == v[i][0]) uz[y] += val;
  }
}
void bkt(int k) {
  if (k > n) {
    _try();
    return;
  }
  int ok = 0;
  for (int i = 0; i < n; i++) {
    int y = i + p[k];
    if (y > n) y -= n;
    if (uz[y]) continue;
    crt[k] = i;
    mark(k, i, 1);
    ok = 1;
    bkt(k + 1);
    mark(k, i, -1);
  }
  if (ok == 0) {
    crt[k] = 0;
    mark(k, 0, 1);
    bkt(k + 1);
    mark(k, 0, -1);
  }
}
bool comp(vector<int> a, vector<int> b) { return a[0] > b[0]; }
int main() {
  ios::sync_with_stdio(false);
  cin >> t;
  while (t--) {
    ans = 0;
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
      v[i].clear();
      v[i].resize(n + 1);
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> v[j][i];
      }
    }
    for (int j = 1; j <= m; j++) {
      int mx = 0;
      for (int i = 1; i <= n; i++) v[j][0] = max(v[j][0], v[j][i]);
    }
    sort(v + 1, v + m + 1, comp);
    if (m < n) {
      for (int i = m + 1; i <= n; i++) {
        v[i].clear();
        v[i].resize(n + 1);
      }
      m = n;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (v[j][i] == v[j][0]) p[j] = i;
      }
    }
    memset(uz, 0, sizeof(uz));
    for (int i = 1; i <= n; i++) {
      if (v[1][i] == v[1][0]) uz[i]++;
      mx[1][i] = v[1][i];
    }
    bkt(2);
    cout << ans << '\n';
  }
  return 0;
}
