#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, a[20][2000 + 10], id[N], maxx[N], dp[1 << 13], olddp[1 << 13],
    temp[1 << 13];
int main() {
  int cas;
  cin >> cas;
  while (cas--) {
    memset(dp, 0, sizeof dp);
    memset(olddp, 0, sizeof olddp);
    memset(maxx, 0, sizeof maxx);
    scanf("%d%d", &n, &m);
    for (int i = (0); i <= (n - 1); i++)
      for (int j = (1); j <= (m); j++)
        scanf("%d", &a[i][j]), maxx[j] = max(maxx[j], a[i][j]);
    for (int i = (1); i <= (m); i++) id[i] = i;
    sort(id + 1, id + 1 + m, [](int x, int y) { return maxx[x] > maxx[y]; });
    m = min(m, n);
    for (int i = (1); i <= (m); i++) {
      memcpy(olddp, dp, sizeof dp);
      memset(temp, 0, sizeof temp);
      for (int state = 0; state < (1 << n); ++state) {
        for (int k = 0; k < n; k++) {
          int sum = 0;
          for (int now = 0; now < n; ++now)
            if (1 & (state >> now)) sum += a[(now + k) % n][id[i]];
          temp[state] = max(temp[state], sum);
        }
        for (int state2 = state; state2 < (1 << n);
             state2 = ((state2 + 1) | state))
          dp[state2] = max(dp[state2], temp[state] + olddp[state2 ^ state]);
      }
    }
    printf("%d\n", dp[(1 << n) - 1]);
  }
  return 0;
}
