#include <bits/stdc++.h>
using namespace std;
inline int gi() {
  int f = 1, sum = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    sum = (sum << 3) + (sum << 1) + ch - '0';
    ch = getchar();
  }
  return f * sum;
}
const int N = 13, M = 2010;
int a[N][M], n, m, f[M][1 << N], b[N][M], pre[M][1 << N];
void turn(int id) {
  int tmp[N];
  for (int i = 0; i < n; i++) tmp[i] = a[i][id];
  for (int i = 0; i < n; i++) a[i][id] = tmp[(i + 1) % n];
}
int Get(int id, int S) {
  int now = 0;
  for (int i = 0; i < n; i++)
    if ((S >> i) & 1) now += a[i][id];
  return now;
}
struct node {
  int id, val;
  bool operator<(const node &b) const { return val > b.val; }
} p[M];
int get(int id, int S) { return pre[id][S]; }
int main() {
  int T = gi();
  while (T--) {
    n = gi();
    m = gi();
    for (int i = 1; i <= m; i++) p[i].val = 0;
    for (int i = 0; i < n; i++)
      for (int j = 1; j <= m; j++) a[i][j] = gi();
    for (int i = 1; i <= m; i++)
      for (int j = 0; j < n; j++)
        p[i].id = i, p[i].val = max(p[i].val, a[j][i]);
    sort(p + 1, p + m + 1);
    for (int j = 1; j <= m; j++)
      for (int i = 0; i < n; i++) b[i][j] = a[i][p[j].id];
    for (int i = 0; i <= n - 1; i++)
      for (int j = 1; j <= m; j++) a[i][j] = b[i][j];
    int ans = 0;
    memset(f, -63, sizeof(f));
    f[0][0] = 0;
    memset(pre, 0, sizeof(pre));
    for (int i = 1; i <= min(n, m); i++)
      for (int j = 0; j < 1 << n; j++)
        for (int l = 0; l < n; l++) {
          pre[i][j] = max(pre[i][j], Get(i, j));
          turn(i);
        }
    for (int i = 1; i <= min(n, m); i++) {
      for (int j = 0; j < 1 << n; j++) {
        for (int k = j;; k = j & (k - 1)) {
          f[i][j] = max(f[i][j], f[i - 1][k] + get(i, j ^ k));
          if (!k) break;
        }
      }
      for (int j = 0; j < 1 << n; j++) ans = max(ans, f[i][j]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
