#include <bits/stdc++.h>
using namespace std;
const int NN = 16, MM = 2004;
struct node {
  int id, p;
  bool operator<(const node& it) const { return p > it.p; }
} num[MM];
int a[NN][MM], d[NN][1 << NN], f[NN][1 << NN];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    memset(d, 0, sizeof(d));
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) scanf("%d", &a[i][j]);
    for (int j = 0; j < m; j++) {
      num[j].p = 0;
      num[j].id = j;
      for (int i = 0; i < n; i++) num[j].p = max(num[j].p, a[i][j]);
    }
    sort(num, num + m);
    m = min(n, m);
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < 1 << n; j++)
        for (int k = 0; k < n; k++)
          if (j >> k & 1) d[i][j] += a[k][num[i].id];
      for (int j = 0; j < 1 << n; j++)
        for (int k = 0; k < n; k++) {
          int t = (j >> k | j << n - k) & (1 << n) - 1;
          d[i][j] = max(d[i][j], d[i][t]);
        }
    }
    for (int i = 0; i < m; i++)
      for (int j = 0; j < 1 << n; j++) {
        f[i + 1][j] = f[i][j];
        for (int t = j; t; t = t - 1 & j)
          f[i + 1][j] = max(f[i + 1][j], f[i][j ^ t] + d[i][t]);
      }
    printf("%d\n", f[m][(1 << n) - 1]);
  }
  return 0;
}
