#include <bits/stdc++.h>
using namespace std;
const int N = 14, M = 2010;
int v[M][N], w[M], g[N][1 << N], f[N][1 << N];
inline bool cmp(int a, int b) { return v[a][0] > v[b][0]; }
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    memset(v, 0, sizeof(v));
    memset(g, 0, sizeof(g));
    memset(f, 0, sizeof(f));
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        scanf("%d", &v[j][i]);
        v[j][0] = max(v[j][0], v[j][i]);
      }
    }
    for (int i = 1; i <= m; i++) w[i] = i;
    sort(w + 1, w + m + 1, cmp);
    m = min(n, m);
    for (int i = 1; i <= m; i++) {
      int p = w[i];
      for (int s = 0; s < (1 << n); s++)
        for (int k = 1; k <= n; k++)
          if (s & (1 << (k - 1))) g[i][s] += v[p][k];
      for (int s = 0; s < (1 << n); s++)
        for (int k = (s >> 1) | ((s & 1) << (n - 1)), cnt = 0; cnt < n;
             k = (k >> 1) | ((k & 1) << (n - 1)), cnt++)
          g[i][s] = max(g[i][s], g[i][k]);
    }
    for (int i = 1; i <= m; i++) {
      for (int s = 0; s < (1 << n); s++) {
        for (int s1 = s; s1; s1 = (s1 - 1) & s)
          f[i][s] = max(f[i][s], f[i - 1][s ^ s1] + g[i][s1]);
        f[i][s] = max(f[i][s], f[i - 1][s]);
      }
    }
    printf("%d\n", f[m][(1 << n) - 1]);
  }
  return 0;
}
