#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const long double eps = 1e-6;
const int mod = 1e9 + 7;
const int maxn = 20;
const int N = 1e5 + 10;
const int M = N << 1;
const long long mm = (1LL << 32);
template <class T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= c == '-';
  for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
  x = f ? -x : x;
}
template <class T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  T y = 1;
  int len = 1;
  for (; y <= x / 10; y *= 10) ++len;
  for (; len; --len, x %= y, y /= 10) putchar(x / y + 48);
}
long long qpow(long long a, long long b, long long MOD) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % MOD;
    b >>= 1;
    a = (a * a) % MOD;
  }
  return ans;
}
int n, m;
int a[20][3000];
struct node {
  int id, key;
  inline bool operator<(const node &x) const { return key > x.key; }
} t[3000];
int f[(1 << 20) + 10], t1[(1 << 20) + 10], t2[(1 << 20) + 10];
bool vis[3000];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    cin >> n >> m;
    for (int i = 1; i <= m; ++i) t[i].id = i, t[i].key = 0, vis[i] = 0;
    for (int i = 0; i < n; ++i)
      for (int j = 1; j <= m; ++j) {
        cin >> a[i][j];
        t[j].key = max(t[j].key, a[i][j]);
      }
    sort(t + 1, t + 1 + m);
    for (int i = 1; i <= m && i <= n; ++i) vis[t[i].id] = 1;
    memset(f, 0, sizeof(f));
    for (int i = 1; i <= m; ++i) {
      if (!vis[i]) continue;
      for (int s = 0; s < (1 << n); ++s) t1[s] = 0;
      for (int j = 1; j <= n; ++j) {
        for (int s = 0; s < (1 << n); ++s) t2[s] = f[s];
        for (int s = 0; s < (1 << n); ++s)
          for (int k = 0; k < n; ++k)
            if (!(s & (1 << k)))
              t2[s | (1 << k)] = max(t2[s | (1 << k)], t2[s] + a[k][i]);
        int tmp = a[0][i];
        for (int k = 0; k < n - 1; ++k) a[k][i] = a[k + 1][i];
        a[n - 1][i] = tmp;
        for (int s = 0; s < (1 << n); ++s) t1[s] = max(t1[s], t2[s]);
      }
      for (int s = 0; s < (1 << n); ++s) f[s] = t1[s];
    }
    cout << f[(1 << n) - 1] << endl;
  }
  return 0;
}
