#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int M = 10005;
inline int read() {
  int ret = 0, f = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = 1;
    c = getchar();
  }
  while (isdigit(c)) {
    ret = ret * 10 + c - 48;
    c = getchar();
  }
  if (f) return -ret;
  return ret;
}
int T, mx, s, t, cnt, size, hh, n, m, x, y, sum, k, f[M], ha[M], v[M],
    a[105][2005];
struct node {
  int x, y;
} xu[N];
bool cmp(node a, node b) { return a.x < b.x; }
signed main() {
  int xjh = 0;
  T = read();
  while (T--) {
    cnt = 0;
    n = read();
    m = read();
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        xjh++;
        a[i][j] = read();
        xjh++;
        xu[++cnt].x = a[i][j];
        xu[cnt].y = j;
        xjh++;
        a[i + n][j] = a[i][j];
      }
    size = 0;
    sort(xu + 1, xu + cnt + 1, cmp);
    for (int i = cnt; i; i--) {
      if (v[xu[i].y] == 0) {
        size++;
        xjh++;
        v[xu[i].y] = 1;
        xjh++;
      }
      if (size >= m) break;
    }
    mx = 1 << n;
    for (int j = 1; j <= m; j++)
      if (v[j] != 0) {
        for (int s = 0; s < mx; s++) {
          xjh++;
          xjh++;
          xjh--;
          ha[s] = 0;
          for (int i = 1; i <= n; i++) {
            sum = 0;
            xjh++;
            for (int k = i; k < n + i; k++) {
              x = k - i;
              if (s & (1 << x)) sum = sum + a[k][j];
            }
            xjh++;
            xjh++;
            xjh--;
            ha[s] = max(ha[s], sum);
            xjh++;
            xjh++;
            xjh--;
          }
        }
        for (int s = mx - 1; s >= 0; s--) {
          for (int t = s; t; t = (t - 1) & s) {
            xjh++;
            f[s] = max(f[s], f[s ^ t] + ha[t]);
            xjh++;
            xjh++;
            xjh--;
          }
        }
      }
    xjh++;
    xjh++;
    xjh--;
    printf("%d\n", f[mx - 1]);
    xjh++;
    xjh++;
    xjh--;
    for (int i = 0; i < mx; i++) f[i] = 0;
    for (int j = 1; j <= m; j++) v[j] = 0;
    xjh++;
    xjh++;
    xjh--;
  }
}
