#include <bits/stdc++.h>
using namespace std;
int dp[13][4400];
int op[4400];
int a[12][2000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  vector<int> ans;
  for (int lel = 0; lel < t; ++lel) {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        cin >> a[i][j];
      }
    }
    vector<pair<int, int>> srt;
    for (int i = 0; i < m; ++i) {
      int loc = 0;
      for (int j = 0; j < n; ++j) {
        loc = max(loc, a[j][i]);
      }
      srt.push_back({loc, i});
    }
    sort((srt).rbegin(), (srt).rend());
    for (int j = 0; j <= min(n, (int)(srt).size()); ++j) {
      for (int i = 0; i < (1 << n); ++i) {
        dp[j][i] = 0;
      }
    }
    for (int i = 0; i < min(n, (int)(srt).size()); ++i) {
      for (int mask = 0; mask < (1 << n); ++mask) {
        op[mask] = 0;
        for (int j = 0; j < n; ++j) {
          int loc = 0;
          for (int d = 0; d < n; ++d) {
            if ((mask & (1 << d)) > 0) {
              loc += a[(j + d) % n][srt[i].second];
            }
          }
          op[mask] = max(op[mask], loc);
        }
      }
      for (int mask = 0; mask < (1 << n); ++mask) {
        dp[i + 1][mask] = dp[i][mask];
      }
      for (int mask = 0; mask < (1 << n); ++mask) {
        int contr = ((1 << n) - 1 - mask);
        for (int cur = contr; cur; cur = ((cur - 1) & contr)) {
          dp[i + 1][(mask + cur)] =
              max(dp[i + 1][(mask + cur)], dp[i][mask] + op[cur]);
        }
      }
    }
    ans.push_back(dp[min((int)(srt).size(), n)][(1 << n) - 1]);
  }
  for (auto elem : ans) {
    cout << elem << "\n";
  }
  return 0;
}
