#include <bits/stdc++.h>
using namespace std;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
int f[20][4100], n, m, a[20][2010], id[2010], mx[2010], val[4100], s[4100];
int main() {
  int T = gi();
  while (T--) {
    memset(f, 0xc0, sizeof(f));
    cin >> n >> m;
    f[0][0] = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) a[i][j] = gi();
    for (int i = 0; i < m; i++) {
      mx[i] = 0;
      for (int j = 0; j < n; j++) mx[i] = max(mx[i], a[j][i]);
      id[i] = i;
    }
    sort(id, id + m, [&](int a, int b) { return mx[a] > mx[b]; });
    for (int i = 0; i < m && i < n; i++) {
      memset(val, 0, sizeof(val));
      int x = id[i];
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) s[1 << k] = a[(j + k) % n][x];
        for (int k = 0; k < (1 << n); k++) {
          if (__builtin_popcount(k) >= 2) s[k] = s[k & -k] + s[k ^ (k & -k)];
          val[k] = max(val[k], s[k]);
        }
      }
      for (int s = 0; s < (1 << n); s++)
        for (int all = (1 << n) - 1, t = all ^ s;; t = (t - 1) & (all ^ s)) {
          f[i + 1][s | t] = max(f[i + 1][s | t], f[i][s] + val[t]);
          if (!t) break;
        }
    }
    cout << f[min(n, m)][(1 << n) - 1] << '\n';
  }
  return 0;
}
