#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n, vector<int>(m));
    for (auto &i : a)
      for (auto &j : i) cin >> j;
    if (m > n) {
      vector<int> mx(m);
      for (int j = 0; j < m; ++j)
        for (int i = 0; i < n; ++i) mx[j] = max(mx[j], a[i][j]);
      vector<int> ord(m);
      iota(ord.begin(), ord.end(), 0);
      sort(ord.begin(), ord.end(),
           [&mx](int x, int y) { return mx[x] > mx[y]; });
      auto b = a;
      for (int j = 0; j < n; ++j)
        for (int i = 0; i < n; ++i) a[i][j] = b[i][ord[j]];
      m = n;
    }
    vector<vector<int>> cost(m, vector<int>(1 << n));
    for (int j = 0; j < m; ++j) {
      vector<int> x(n);
      for (int i = 0; i < n; ++i) x[i] = a[i][j];
      for (int k = 0; k < n; ++k) {
        for (int i = 1; i < 1 << n; ++i) {
          int v = 0;
          for (int z = 0; z < n; ++z)
            if (i >> z & 1) v += x[z];
          cost[j][i] = max(cost[j][i], v);
        }
        rotate(x.begin(), x.begin() + 1, x.end());
      }
    }
    vector<vector<int>> dp(m, vector<int>(1 << n));
    dp[0] = cost[0];
    for (int i = 0; i + 1 < m; ++i) {
      dp[i + 1] = dp[i];
      for (int j = 0; j < 1 << n; ++j) {
        int mask = ((1 << n) - 1) ^ j;
        for (int s = mask; s > 0; s = (s - 1) & mask)
          dp[i + 1][j | s] = max(dp[i + 1][j | s], dp[i][j] + cost[i + 1][s]);
      }
    }
    cout << dp[m - 1][(1 << n) - 1] << "\n";
  }
  return 0;
}
