#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0;
  char ch = getchar();
  bool f = 0;
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') f = 1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 3) + (x << 1) + (ch ^ 48);
  return f ? -x : x;
}
struct node {
  int b[13], mx;
  friend inline bool operator<(const node &A, const node &B) {
    return A.mx > B.mx;
  }
} a[1 << 13];
int n, m;
inline int calc(const node &A, int s) {
  int mx = 0, sum = 0;
  for (int i = 0; i < n; i++) {
    sum = 0;
    for (int j = 0; j < n; j++)
      if ((1 << j) & s) sum += A.b[(i + j) % n];
    mx = max(mx, sum);
  }
  return mx;
}
int f[1 << 13], g[13][1 << 13];
int main() {
  for (int _ = read(); _--;) {
    n = read();
    m = read();
    for (int i = 1; i <= m; i++) a[i].mx = 0;
    for (int i = 0; i < n; i++)
      for (int j = 1; j <= m; j++) a[j].mx = max(a[j].mx, a[j].b[i] = read());
    sort(a + 1, a + m + 1);
    int cnt = 1 << n;
    for (int i = 1; i <= min(n, m); i++) {
      for (int j = 0; j < cnt; j++) f[j] = calc(a[i], j);
      for (int S = 0; S < cnt; S++) {
        g[i][S] = f[S];
        for (int s = S; s; s = (s - 1) & S)
          g[i][S] = max(g[i][S], g[i - 1][s] + f[s ^ S]);
      }
    }
    printf("%d\n", g[min(n, m)][cnt - 1]);
  }
  return 0;
}
