#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const long long N = 1e5 + 10;
const long long inf = 1e18;
const long long mod1 = 1e9 + 7;
const long long mod2 = 998244353;
const long long base = 137;
const long long w = 26;
vector<long long> cycle(vector<long long> a) {
  vector<long long> b(a.size());
  for (long long i = 1; i < a.size(); ++i) b[i] = a[i - 1];
  b[0] = a[a.size() - 1];
  return b;
}
bool cp(vector<long long> &x, vector<long long> &y) {
  long long X = 0;
  long long Y = 0;
  for (long long &i : x) X = max(X, i);
  for (long long &i : y) Y = max(Y, i);
  return X > Y;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    vector<long long> a[m];
    for (long long i = 0; i < m; ++i) a[i].resize(n);
    for (long long i = 0; i < n; ++i) {
      for (long long j = 0; j < m; ++j) {
        cin >> a[j][i];
      }
    }
    sort(a, a + m, cp);
    long long c = min(n, m);
    long long s[c][(1 << n)];
    for (long long i = 0; i < c; ++i) {
      fill(s[i], s[i] + (1 << n), 0);
      for (long long c = 0; c < n; ++c) {
        for (long long mask = 0; mask < (1 << n); ++mask) {
          long long sum = 0;
          for (long long k = 0; k < n; ++k) {
            if ((mask >> k) & 1) sum += a[i][k];
          }
          s[i][mask] = max(s[i][mask], sum);
        }
        a[i] = cycle(a[i]);
      }
    }
    long long dp[(1 << n)];
    fill(dp, dp + (1 << n), 0);
    for (long long j = 0; j < (1 << n); ++j) dp[j] = s[0][j];
    for (long long i = 1; i < c; ++i) {
      vector<long long> ba(1 << n, 0);
      for (long long j = 0; j < (1 << n); ++j) ba[j] = dp[j];
      for (long long mask = 0; mask < (1 << n); ++mask) {
        for (long long mask2 = mask;; mask2 = (mask2 - 1) & mask) {
          ba[mask] = max(ba[mask], s[i][mask2] + dp[~mask2 & mask]);
          if (mask2 == 0) break;
        }
      }
      for (long long j = 0; j < (1 << n); ++j) dp[j] = ba[j];
    }
    cout << dp[(1 << n) - 1] << endl;
  }
}
