#include <bits/stdc++.h>
using namespace std;
const int QQ = 3e5 + 10;
const int maxN = 2e5;
const int maxM = 3e5 + 10;
const int oo = 5e3;
const int module = 1e9 + 7;
const long double du = 1e-9;
void doc() {
  freopen(
      "solve"
      ".inp",
      "r", stdin);
  freopen(
      "solve"
      ".out",
      "w", stdout);
}
int Get() {
  int n, m;
  cin >> n >> m;
  int a[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> a[i][j];
  vector<pair<int, int> > A;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      A.push_back(pair<int, int>(a[i][j], j));
    }
  }
  sort(A.begin(), A.end(), greater<pair<int, int> >());
  bool KT[m];
  memset(KT, 0, sizeof(KT));
  int cnt = 0;
  for (pair<int, int> x : A) {
    if (KT[x.second] == 0) {
      KT[x.second] = 1;
      cnt++;
      if (cnt == n) break;
    }
  }
  int F[1 << n];
  memset(F, 0, sizeof(F));
  int Mask = 1 << n;
  vector<int> Pos[Mask];
  for (int i = 0; i < Mask; i++) {
    for (int j = 0; j < n; j++) {
      int bit = (i >> j) & 1;
      if (bit) Pos[i].push_back(j);
    }
  }
  for (int j = 0; j < m; j++) {
    if (KT[j] == 0) continue;
    int nF[Mask];
    memset(nF, 0, sizeof(nF));
    for (int mask = 1; mask < Mask; mask++) {
      for (int sl = 0; sl < n; sl++) {
        int S = 0;
        for (int id : Pos[mask]) S += a[(id + sl) % n][j];
        nF[mask] = max(nF[mask], S);
      }
    }
    for (int t = (1 << n) - 1; t >= 0; t--) {
      int u = t;
      while (u > 0) {
        F[t] = max(F[t], F[t ^ u] + nF[u]);
        if (u == 0) break;
        u = (u - 1) & t;
      }
    }
  }
  return F[Mask - 1];
}
void solve() {
  int Test;
  cin >> Test;
  while (Test--) cout << Get() << '\n';
}
int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  solve();
}
