#include <bits/stdc++.h>
using namespace std;
int n, m;
struct column {
  int a[13];
  int x;
};
column c[2001];
bool cmp(column p, column q) { return p.x > q.x; }
int s[13][4096], f[13][4096];
int mian() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &c[j].a[i]);
  for (int j = 1; j <= m; j++) {
    c[j].x = 0;
    for (int i = 1; i <= n; i++) c[j].x = max(c[j].x, c[j].a[i]);
  }
  sort(c + 1, c + 1 + m, cmp);
  m = min(m, n);
  for (int i = 1; i <= m; i++)
    for (int j = 0; j < (1 << n); j++) {
      s[i][j] = 0;
      for (int p = 0; p < n; p++) {
        int sum = 0;
        for (int l = 1, k = p + 1; l <= n; l++, k = (k == n) ? 1 : (k + 1))
          if (j & (1 << l - 1)) sum += c[i].a[k];
        s[i][j] = max(s[i][j], sum);
      }
    }
  for (int i = 1; i <= m; i++)
    for (int j = 0; j < (1 << n); j++) {
      f[i][j] = f[i - 1][j];
      for (int k = j; k; k = (k - 1) & j)
        f[i][j] = max(f[i][j], f[i - 1][j ^ k] + s[i][k]);
    }
  printf("%d\n", f[m][(1 << n) - 1]);
  return 0;
}
int t;
int main() {
  for (scanf("%d", &t); t; t--) mian();
  return 0;
}
