#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char c = getchar();
  int x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
int f[14][1 << 12], lg2[1 << 12], sum[1 << 12], tmp[1 << 12];
struct column {
  int mx, x[13];
  bool operator<(const column& b) const { return mx > b.mx; }
} a[2005];
int n, m;
void work() {
  n = read(), m = read();
  for (register int i = (0); i <= (n - 1); ++i) lg2[1 << i] = i;
  for (register int j = (1); j <= (m); ++j) a[j].mx = 0;
  for (register int i = (1); i <= (n); ++i)
    for (register int j = (1); j <= (m); ++j)
      a[j].x[i] = read(), a[j].mx = max(a[j].mx, a[j].x[i]);
  sort(a + 1, a + m + 1);
  memset(f, 0, sizeof f);
  int maxst = (1 << n) - 1;
  for (register int i = (1); i <= (min(n, m)); ++i) {
    for (register int s = (0); s <= (maxst); ++s) {
      tmp[s] = 0;
      for (register int d = (0); d <= (n - 1); ++d) {
        int now = 0;
        for (int t = s; t; t ^= t & -t)
          now += a[i].x[(d + lg2[t & -t]) % n + 1];
        tmp[s] = max(tmp[s], now);
      }
      f[i][s] = f[i - 1][0] + tmp[s];
    }
    for (register int s = (0); s <= (maxst); ++s)
      for (int t = s; t; t = (t - 1) & s)
        f[i][s] = max(f[i][s], f[i - 1][t] + tmp[s ^ t]);
  }
  cout << f[min(n, m)][maxst] << endl;
}
int main() {
  int T = read();
  while (T--) work();
  return 0;
}
