#include <bits/stdc++.h>
using namespace std;
int a[16][2048];
int n, m;
pair<int, int> w[2048];
int b[12][12];
int f[1 << 12];
int f2[1 << 12];
int p[12][1 << 12];
int main() {
  int tn;
  scanf("%d", &tn);
  while (tn--) {
    scanf("%d%d", &n, &m);
    memset(a, 0, sizeof(a));
    for (int i = 0, ThxDem = n; i < ThxDem; ++i)
      for (int j = 0, ThxDem = m; j < ThxDem; ++j) scanf("%d", &a[i][j]);
    m = max(m, n);
    for (int j = 0, ThxDem = m; j < ThxDem; ++j) {
      int mx = -1;
      for (int i = 0, ThxDem = n; i < ThxDem; ++i) mx = max(mx, a[i][j]);
      w[j] = make_pair(mx, j);
    }
    sort(w, w + m);
    reverse(w, w + m);
    for (int i = 0, ThxDem = n; i < ThxDem; ++i)
      for (int j = 0, ThxDem = n; j < ThxDem; ++j) b[i][j] = a[i][w[j].second];
    memset(f, 0, sizeof(f));
    for (int j = 0, ThxDem = n; j < ThxDem; ++j)
      for (int s = 0, ThxDem = (1 << n); s < ThxDem; ++s) {
        int r = 0;
        for (int o = 0, ThxDem = n; o < ThxDem; ++o) {
          int x = 0;
          for (int i = 0, ThxDem = n; i < ThxDem; ++i)
            if (s & (1 << i)) x += b[(i + o) % n][j];
          r = max(r, x);
        }
        p[j][s] = r;
      }
    for (int j = 0, ThxDem = n; j < ThxDem; ++j) {
      memset(f2, 0, sizeof(f2));
      for (int m = 0, ThxDem = (1 << n); m < ThxDem; ++m) {
        int w = ((1 << n) - 1) ^ m;
        for (int s = w;; s = ((s - 1) & w)) {
          f2[m | s] = max(f2[m | s], f[m] + p[j][s]);
          if (!s) break;
        }
      }
      memcpy(f, f2, sizeof(f));
    }
    printf("%d\n", f[(1 << n) - 1]);
  }
  return 0;
}
