#include <bits/stdc++.h>
using namespace std;
struct note {
  int a[15], maxv;
  bool operator<(const note& A) const { return maxv > A.maxv; }
} B[2003];
int n, m, t, dp[15][4100], f[15][4100];
void work() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &B[j].a[i]);
  for (int j = 1; j <= m; j++) {
    B[j].maxv = 0;
    for (int i = 1; i <= n; i++) B[j].maxv = max(B[j].maxv, B[j].a[i]);
  }
  sort(B + 1, B + m + 1);
  int maxn = (1 << n) - 1;
  m = min(n, m);
  for (int j = 1; j <= m; j++) {
    memset(f[j], 0, sizeof f[j]);
    for (int s = 0; s <= maxn; s++)
      for (int k = 0; k < n; k++)
        if (s & (1 << k)) f[j][s] += B[j].a[k + 1];
    for (int s = 0; s <= maxn; s++)
      for (int k = 0; k < n; k++) {
        int t = ((s << k) | (s >> n - k)) & maxn;
        f[j][s] = max(f[j][s], f[j][t]);
      }
    memset(dp[j], 0, sizeof dp[j]);
    for (int s = 0; s <= maxn; s++) {
      for (int l = s;; l = (l - 1) & s) {
        dp[j][s] = max(dp[j][s], dp[j - 1][l] + f[j][s ^ l]);
        if (l == 0) break;
      }
    }
  }
  printf("%d\n", dp[m][maxn]);
}
int main() {
  scanf("%d", &t);
  while (t--) work();
  return 0;
}
