#include <bits/stdc++.h>
using namespace std;
const int MAXN = 13;
const int MAXM = 2005;
int B[MAXM], C[MAXM], A[MAXN][MAXM], g[MAXN][(1 << MAXN) + 5],
    f[MAXN][(1 << MAXN) + 5];
bool cmp(int a, int b) { return B[a] > B[b]; }
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) B[i] = 0, C[i] = i;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        scanf("%d", &A[i][j]);
        B[j] = max(B[j], A[i][j]);
      }
    sort(C + 1, C + 1 + m, cmp);
    int S = (1 << n) - 1, o = min(n, m);
    for (int i = 1; i <= o; i++) {
      int k = C[i];
      for (int j = 0; j <= S; j++) {
        int u = 0;
        for (int t = 0; t < n; t++) {
          int r = 0;
          for (int z = 1; z <= n; z++) {
            int G = ((z - 1) + t) % n + 1;
            if ((1 << (G - 1)) & j) r += A[z][k];
          }
          u = max(u, r);
        }
        g[i][j] = u;
      }
    }
    for (int i = 1; i <= o; i++)
      for (int j = 0, k = 0; j <= S; j++)
        for (f[i][j] = g[i][j], k = j; k; k = (k - 1) & j)
          f[i][j] = max(f[i][j], f[i - 1][k] + g[i][j - k]);
    printf("%d\n", f[o][S]);
  }
  return 0;
}
