#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int maxN = 2000 + 9;
const int maxB = 15;
long long f[maxB][maxB][1 << maxB], gg[1 << maxB];
int n, m;
long long cell[maxB][maxN];
vector<pair<long long, int> > col;
void read() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> cell[i][j];
    }
  }
  col.assign(m, make_pair(0LL, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      col[j] = max(col[j], make_pair(cell[i][j], j));
    }
  }
}
void preCal() {
  sort(col.begin(), col.end());
  reverse(col.begin(), col.end());
  for (int run = 0; run < min(n, m); run++) {
    int cur = col[run].second;
    for (int s = 0; s < n; s++) {
      for (int mask = 0; mask < (1 << n); mask++) {
        f[run][s][mask] = 0;
        for (int i = 0; i < n; i++) {
          if ((mask & (1 << i)) > 0) {
            int j = (s + i) % n;
            f[run][s][mask] += cell[j][cur];
          }
        }
      }
    }
  }
}
long long memo[maxB][1 << maxB];
void solve() {
  for (int i = 0; i <= min(n, m); i++) {
    for (int j = 0; j < (1 << n); j++) {
      memo[i][j] = 0;
    }
  }
  int all = (1 << n) - 1;
  preCal();
  for (int i = min(n, m) - 1; i >= 0; i--) {
    for (int bitmask = 0; bitmask <= all; bitmask++) {
      gg[bitmask] = 0;
      for (int shift = 0; shift < n; shift++) {
        gg[bitmask] = max(gg[bitmask], f[i][shift][bitmask]);
      }
    }
    for (int bitmask = 0; bitmask <= all; bitmask++) {
      int rem = all ^ bitmask;
      for (int choose = rem;; choose = (choose - 1) & rem) {
        memo[i][bitmask] =
            max(memo[i][bitmask], memo[i + 1][bitmask | choose] + gg[choose]);
        if (choose == 0) {
          break;
        }
      }
    }
  }
  cout << memo[0][0] << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int nTest;
  cin >> nTest;
  while (nTest--) {
    read();
    solve();
  }
  return 0;
}
