#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char i = getchar();
  long long f = 1, res = 0;
  while (i < '0' || i > '9') {
    if (i == '-') f = -1;
    i = getchar();
  }
  while (i >= '0' && i <= '9') {
    res = res * 10 + i - '0';
    i = getchar();
  }
  return res * f;
}
int n, m;
const int N = 1e5 + 50;
struct zk {
  int a[25], maxn;
  inline void init(int* x) {
    memset(a, 0, sizeof a);
    maxn = 0;
    for (int i = 0; i < n; ++i) {
      a[i] = x[i];
      maxn = max(maxn, x[i]);
    }
  }
  bool operator<(const zk& A) const { return maxn > A.maxn; }
} line[N];
int rd[N][25];
long long dp[5000];
long long tmp[5000], odp[5000];
int main() {
  int T = read();
  while (T--) {
    memset(dp, 0, sizeof dp);
    n = read(), m = read();
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) rd[j][i] = read();
    for (int i = 0; i < m; ++i) {
      line[i].init(rd[i]);
    }
    sort(line, line + m);
    m = min(n, m);
    int sz = (1 << n);
    for (int i = 0; i < m; ++i) {
      memset(tmp, 0, sizeof tmp);
      for (int st = 0; st < sz; ++st) {
        for (int k = 0; k < n; ++k) {
          long long res = 0;
          for (int now = 0; now < n; ++now) {
            if (st & (1 << now)) {
              res += line[i].a[(now + k) % n];
            }
            tmp[st] = max(tmp[st], res);
          }
        }
      }
      memcpy(odp, dp, sizeof dp);
      for (int st = 0; st < sz; ++st) {
        for (int k = st; k < sz; k = (k + 1) | st) {
          dp[k] = max(dp[k], odp[st ^ k] + tmp[st]);
        }
      }
    }
    printf("%lld\n", dp[sz - 1]);
  }
}
