#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int> > arr(m, vector<int>(n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> arr[j][i];
  auto cmp = [&](vector<int> a, vector<int> b) {
    return *max_element(a.begin(), a.end()) > *max_element(b.begin(), b.end());
  };
  sort(arr.begin(), arr.end(), cmp);
  m = min(m, n);
  int MASKS = 1 << n;
  vector<vector<int> > cost(m, vector<int>(MASKS, 0));
  for (int i = 0; i < m; i++) {
    for (int mask = 0; mask < MASKS; mask++) {
      int& cur = cost[i][mask];
      for (int rot = 0; rot < n; rot++) {
        int sum = 0;
        for (int k = 0; k < n; k++) {
          if ((1 << k) & mask) {
            int idx = rot + k;
            if (idx >= n) idx -= n;
            sum += arr[i][idx];
          }
        }
        cur = max(cur, sum);
      }
    }
  }
  vector<vector<int> > dp(m + 1, vector<int>(MASKS, 0));
  for (int i = 1; i <= m; i++) {
    for (int mask = 0; mask < MASKS; mask++) {
      int& cur = dp[i][mask];
      cur = dp[i - 1][mask];
      for (int subset = mask; subset; subset = (subset - 1) & mask) {
        cur = max(cur, dp[i - 1][mask - subset] + cost[i - 1][subset]);
      }
    }
  }
  cout << dp[m][MASKS - 1] << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
