#include <bits/stdc++.h>
using namespace std;
int n, m, S;
int a[15][2005], mx[2005], id[2005], b[15][15];
int f[15][(1 << 13) + 5], w[15][(1 << 13) + 5];
bool cmp(int x, int y) { return mx[x] > mx[y]; }
void calc(int x, int s) {
  w[x][s] = 0;
  for (int j = 0; j < n; j++) {
    int t = 0;
    for (int i = 1; i <= n; i++) {
      if (s & (1 << (i - 1))) {
        t += b[(i + j - 1) % n + 1][x];
      }
    }
    w[x][s] = max(w[x][s], t);
  }
}
int main() {
  int T;
  cin >> T;
  for (int num = 1; num <= T; num++) {
    memset(f, 0, sizeof(f));
    memset(w, 0, sizeof(w));
    memset(mx, 0, sizeof(mx));
    cin >> n >> m;
    S = (1 << n) - 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        scanf("%d", &a[i][j]);
      }
    }
    for (int i = 1; i <= m; i++) {
      mx[i] = 0, id[i] = i;
      for (int j = 1; j <= n; j++) mx[i] = max(mx[i], a[j][i]);
    }
    sort(id + 1, id + m + 1, cmp);
    m = min(m, n);
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j <= n; j++) {
        b[j][i] = a[j][id[i]];
      }
    }
    for (int i = 1; i <= m; i++) {
      w[i][0] = 0;
      for (int j = 1; j < (1 << n); j++) {
        calc(i, j);
      }
    }
    for (int i = 1; i <= m; i++) {
      for (int j = 0; j <= S; j++) {
        f[i][j] = 0;
        for (int k = j; k >= 0; k = (k - 1) & j) {
          f[i][j] = max(f[i - 1][k] + w[i][j ^ k], f[i][j]);
          if (!k) break;
        }
      }
    }
    cout << f[m][S] << endl;
  }
  return 0;
}
