#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
const int maxn = 2e3 + 100;
const int mod = 1e9 + 7;
int n, m, arr[25][maxn], dp[1 << 12], new_dp[1 << 12], mx[maxn][1 << 12];
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    cin >> n >> m;
    vector<pii> v;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) cin >> arr[i][j];
    for (int j = 0; j < m; j++) {
      int maxi = 0;
      for (int i = 0; i < n; i++) maxi = max(maxi, arr[i][j]);
      v.push_back({maxi, j});
    }
    sort(v.begin(), v.end(), greater<pii>());
    for (int j = 0; j < m; j++) {
      for (int a = n; a < 2 * n; a++) arr[a][j] = arr[a - n][j];
    }
    for (int it = 0; it < min(m, 20); it++) {
      int j = v[it].second;
      for (int i = 0; i < 1 << n; i++) {
        int best = 0;
        for (int a = 0; a < n; a++) {
          int cur = 0;
          for (int bit = 0; bit < n; bit++) {
            if (i >> bit & 1) cur += arr[a + bit][j];
          }
          best = max(best, cur);
        }
        mx[j][i] = best;
      }
    }
    for (int i = 0; i < 1 << n; i++) dp[i] = new_dp[i] = -mod;
    dp[0] = 0;
    for (int it = 0; it < min(m, 20); it++) {
      int j = v[it].second;
      for (int mask = 0; mask < 1 << n; mask++) new_dp[mask] = dp[mask];
      for (int mask = 0; mask < 1 << n; mask++) {
        int msk = (1 << n) - 1 - mask;
        for (int take = msk; take > 0; take = (take - 1) & msk) {
          new_dp[mask + take] =
              max(new_dp[mask + take], dp[mask] + mx[j][take]);
        }
      }
      for (int mask = 0; mask < 1 << n; mask++)
        dp[mask] = max(dp[mask], new_dp[mask]);
    }
    cout << dp[(1 << n) - 1] << '\n';
  }
  return 0;
}
