#include <bits/stdc++.h>
using namespace std;
int dp[13][1 << 12];
int arr[12][2000], a[12][12];
void test_case() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> h;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> arr[i][j];
      h.emplace_back(arr[i][j], j);
    }
  }
  sort(h.begin(), h.end());
  reverse(h.begin(), h.end());
  set<int> s;
  for (auto x : h) {
    s.insert(x.second);
    if (s.size() >= n) {
      break;
    }
  }
  int curind = 0;
  m = s.size();
  for (auto pot : s) {
    for (int i = 0; i < n; i++) {
      a[i][curind] = arr[i][pot];
    }
    curind++;
  }
  memset(dp, 0, sizeof(dp));
  for (int i = m - 1; i >= 0; i--) {
    for (int j = (1 << n) - 1; j >= 0; j--) {
      dp[i][j] = max(dp[i][j], dp[i + 1][j]);
      for (int ind = 0; ind < n; ind++) {
        if (!(j & (1 << ind))) {
          dp[i][j] = max(dp[i][j], dp[i][j | (1 << ind)] + a[ind][i]);
        }
      }
    }
    for (int j = 0; j < (1 << n); j++) {
      int cur = j, mx = 0;
      for (int k = 0; k < n; k++) {
        mx = max(mx, dp[i][cur]);
        int temp = cur & 1;
        cur >>= 1;
        cur |= (temp << (n - 1));
      }
      dp[i][j] = mx;
    }
  }
  cout << dp[0][0] << endl;
  return;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    test_case();
  }
  return 0;
}
