#include <bits/stdc++.h>
using namespace std;
const int Maxn = 12;
const int Maxm = 2000;
int N, M;
int A[Maxn + 5][Maxm + 5];
struct Node {
  int key, id;
};
bool operator<(Node lhs, Node rhs) { return lhs.key > rhs.key; }
Node t[Maxm + 5];
int f[(1 << Maxn) + 5], t1[(1 << Maxn) + 5], t2[(1 << Maxn) + 5];
bool used[Maxm + 5];
int log_2[(1 << Maxn) + 5];
int main() {
  int _;
  scanf("%d", &_);
  while (_--) {
    scanf("%d %d", &N, &M);
    for (int i = 1; i <= M; i++) t[i].id = i, t[i].key = 0, used[i] = false;
    for (int i = 0; i < N; i++)
      for (int j = 1; j <= M; j++) {
        scanf("%d", &A[i][j]);
        t[j].key = max(t[j].key, A[i][j]);
      }
    for (int i = 0; i < N; i++) log_2[1 << i] = i;
    sort(t + 1, t + M + 1);
    for (int i = 1; i <= M && i <= N; i++) used[t[i].id] = true;
    memset(f, 0, sizeof f);
    for (int i = 1; i <= M; i++) {
      if (!used[i]) continue;
      for (int s = 0; s < (1 << N); s++) t1[s] = 0;
      for (int j = 1; j <= N; j++) {
        for (int s = 0; s < (1 << N); s++) t2[s] = f[s];
        for (int s = 0; s < (1 << N); s++) {
          int s1 = ((1 << N) - 1) ^ s;
          while (s1) {
            int x = (s1 & (-s1)), y = log_2[x];
            t2[s | x] = max(t2[s | x], t2[s] + A[y][i]);
            s1 -= x;
          }
        }
        int tmp = A[0][i];
        for (int k = 0; k < N - 1; k++) A[k][i] = A[k + 1][i];
        A[N - 1][i] = tmp;
        for (int s = 0; s < (1 << N); s++) t1[s] = max(t1[s], t2[s]), t2[s] = 0;
      }
      for (int s = 0; s < (1 << N); s++) f[s] = t1[s];
    }
    printf("%d\n", f[(1 << N) - 1]);
  }
  return 0;
}
