#include <bits/stdc++.h>
using namespace std;
const int N = 6e+5;
int a[15][2005];
int dp[15][N], cost[15][N];
int n, m;
bool comp(vector<int> a, vector<int> b) {
  int m1 = 0, m2 = 0;
  for (int i = 0; i < n; i++) m1 = max(m1, a[i]), m2 = max(m2, b[i]);
  return m1 > m2;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    vector<int> v[m + 1];
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        cin >> a[i][j];
        v[j].push_back(a[i][j]);
      }
    sort(v + 1, v + m + 1, comp);
    m = min(n, m);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) a[i][j] = v[j][i - 1];
    for (int i = 1; i <= m; i++) {
      for (int mask = 0; mask < (1 << n); mask++) {
        cost[i][mask] = dp[i][mask] = 0;
        for (int s = 0; s < n; s++) {
          int tans = 0;
          for (int x = 0; x < n; x++) {
            int g = (1 << x);
            if ((mask & g) != 0) {
              tans += a[(x - s + n) % n + 1][i];
            }
          }
          cost[i][mask] = max(cost[i][mask], tans);
        }
      }
    }
    for (int mask = 0; mask < (1 << n); mask++) {
      for (int i = 1; i <= m; i++) {
        dp[i][mask] = cost[i][mask];
        for (int s = mask; s > 0; s = ((s - 1) & mask)) {
          dp[i][mask] =
              max(dp[i][mask], cost[i][mask & (s ^ mask)] + dp[i - 1][s]);
        }
      }
    }
    cout << dp[m][((1 << n) - 1)] << "\n";
  }
}
