#include <bits/stdc++.h>
using namespace std;
template <typename U, typename V>
void Min(U &a, const V &b) {
  if (a > b) a = b;
}
template <typename U, typename V>
void Max(U &a, const V &b) {
  if (a < b) a = b;
}
template <typename U, typename V>
void add(U &a, const V &b) {
  a = (a + b) % 1000000007;
}
template <typename U>
U gcd(U a, U b) {
  if (a == 0) return b;
  if (b == 0) return a;
  if (a >= b)
    return gcd(a - b, b);
  else
    return gcd(a, b - a);
}
int pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % 1000000007;
    a = 1LL * a * a % 1000000007;
    b >>= 1;
  }
  return ans;
}
int a[2001][12], dp[1 << 12];
pair<int, int> b[2001];
int main() {
  int T, m, n, i, j, k, K;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &m);
    int sz = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) scanf("%d", &a[j][i]);
    for (int i = 0; i < m; i++) {
      b[i] = {0, i};
      for (int j = 0; j < n; j++) Max(b[i].first, a[i][j]);
    }
    sort(b, b + m);
    reverse(b, b + m);
    Min(m, n);
    int mask = (1 << n) - 1;
    for (int i = 0; i < mask + 1; i++) dp[i] = 0;
    for (int it = 0; it < m; it++) {
      i = b[it].second;
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < (1 << n); k++)
          if (!(k >> j & 1)) Max(dp[k | 1 << j], dp[k] + a[i][j]);
      }
      for (int s = 0; s < (1 << n); s++) {
        int K = 0, r = s;
        for (int i = 0; i < n; i++) {
          Max(K, dp[r]);
          r = (r >> 1) | ((r & 1) << n - 1);
        }
        for (int i = 0; i < n; i++) {
          dp[r] = K;
          r = (r >> 1) | ((r & 1) << n - 1);
        }
      }
    }
    printf("%d\n", dp[(1 << n) - 1]);
  }
}
