#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int nm = 0, fh = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') fh = -1;
  for (; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
  return nm * fh;
}
struct nd {
  int x, vl;
} sta[5020 << 4];
int n, m, top, topn, a[15][5020], b[15][5020], c[5020], d[5020], num[5020],
    mx[5020], F[5020], G[5020];
bool vis[5020];
inline bool cmp(nd a, nd b) { return a.vl > b.vl; }
inline void solve() {
  n = read(), m = read(), top = topn = 0, memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      a[i][j] = read(), sta[++top] = (nd){j, a[i][j]};
  sort(sta + 1, sta + top + 1, cmp);
  int hvt = min(n, m);
  for (int i = 1; i <= top; i++)
    if (!vis[sta[i].x]) {
      vis[sta[i].x] = true, num[++topn] = sta[i].x;
      if (topn == hvt) break;
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= topn; j++) b[i][j] = a[i][num[j]];
  memset(F, 0, sizeof(F));
  for (int i = 1; i <= topn; i++) {
    for (int j = 1; j <= n; j++) c[j] = b[j][i];
    for (int j = 1; j <= n; j++) c[j + n] = c[j];
    memset(mx, 0, sizeof(mx));
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) d[k] = c[j + k - 1];
      for (int k = 0; k < (1 << n); k++) {
        int ret = 0;
        for (int l = 1; l <= n; l++)
          if (k & (1 << l - 1)) ret += d[l];
        mx[k] = max(mx[k], ret);
      }
    }
    memcpy(G, F, sizeof(G));
    for (int s = 0; s < (1 << n); s++) {
      G[s] = max(G[s], mx[s]);
      for (int j = s & (s - 1); j; j = s & (j - 1))
        G[s] = max(G[s], F[j] + mx[s ^ j]);
    }
    memcpy(F, G, sizeof(F));
  }
  printf("%d\n", F[(1 << n) - 1]);
}
int main() {
  for (int Cas = read(); Cas; Cas--) solve();
  return 0;
}
