#include <bits/stdc++.h>
using namespace std;
struct S {
  int c[13], ma;
} a[2010];
int b[20][2020];
bool cmp(S a, S b) { return a.ma > b.ma; }
int dp[20][(1 << 12) + 10], sum[20][(1 << 12) + 10];
int main() {
  int T, n, m, i, j, k, l;
  scanf("%d", &T);
  while (T--) {
    memset(dp, 0, sizeof dp);
    scanf("%d%d", &n, &m);
    for (i = 1; i <= n; i++)
      for (j = 1; j <= m; j++) scanf("%d", &b[i][j]);
    for (i = 1; i <= m; i++) {
      a[i].ma = 0;
      for (j = 1; j <= n; j++) {
        a[i].ma = max(a[i].ma, b[j][i]);
        a[i].c[j] = b[j][i];
      }
    }
    sort(a + 1, a + 1 + m, cmp);
    m = min(n, m);
    for (i = 1; i <= m; i++)
      for (j = 0; j < (1 << n); j++) {
        int cc = 0;
        for (k = 1; k <= n; k++) {
          int ans = 0;
          for (l = 0; l < n; l++)
            if (j >> ((l + k) % n) & 1) ans += a[i].c[l + 1];
          cc = max(ans, cc);
        }
        sum[i][j] = cc;
      }
    for (i = 1; i <= m; i++)
      for (j = 0; j < (1 << n); j++)
        for (k = j;; k = (k - 1) & j) {
          dp[i][j] = max(dp[i][j], dp[i - 1][k] + sum[i][j ^ k]);
          if (!k) break;
        }
    printf("%d\n", dp[m][(1 << n) - 1]);
  }
}
