#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int n, m, a[15][N], b[15][N], f[15][20005], F[15][20005], c[20005][15];
struct node {
  int val, id;
};
bool operator<(node a, node b) { return a.val > b.val; }
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline int gc() {
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  int ret = 0, f = 0;
  char c = gc();
  while (!isdigit(c)) {
    if (c == '-') f = 1;
    c = gc();
  }
  while (isdigit(c)) {
    ret = ret * 10 + c - 48;
    c = gc();
  }
  if (f) return -ret;
  return ret;
}
void work(int id) {
  for (int i = n; i; i--) b[i + 1][id] = b[i][id];
  b[1][id] = b[n + 1][id];
  for (int i = 0; i < (1 << n); i++) {
    int res = 0;
    for (int j = 1; j <= c[i][0]; j++) res += b[c[i][j]][id];
    f[id][i] = max(f[id][i], res);
  }
}
signed main() {
  int q = read();
  while (q--) {
    n = read();
    m = read();
    for (int i = 0; i < (1 << n); i++) c[i][0] = 0;
    for (int i = 0; i < (1 << n); i++)
      for (int j = 1; j <= n; j++)
        if (i & (1 << (j - 1))) {
          c[i][0]++;
          c[i][c[i][0]] = j;
        }
    for (int i = 1; i <= n; i++)
      for (int j = 0; j < (1 << n); j++) f[i][j] = F[i][j] = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) a[i][j] = read();
    priority_queue<node> q;
    for (int i = 1; i <= m; i++) {
      int ma = 0;
      for (int j = 1; j <= n; j++) ma = max(ma, a[j][i]);
      if (q.size() == n) {
        if (q.top().val < ma) {
          q.pop();
          q.push((node){ma, i});
        }
      } else
        q.push((node){ma, i});
    }
    int gs = 0;
    while (!q.empty()) {
      int id = q.top().id;
      q.pop();
      gs++;
      for (int i = 1; i <= n; i++) b[i][gs] = a[i][id];
    }
    for (int i = 1; i <= gs; i++) {
      for (int j = 1; j <= n + 1; j++) {
        work(i);
      }
    }
    for (int i = 0; i < (1 << n); i++) F[1][i] = f[1][i];
    int xu = (1 << n) - 1;
    for (int i = 2; i <= gs; i++) {
      for (int j = 0; j < (1 << n); j++) {
        int S = j ^ xu;
        for (int k = S; k >= 0; k = (k - 1) & S) {
          F[i][j | k] = max(F[i][j | k], F[i - 1][j] + f[i][k]);
          if (k == 0) break;
        }
      }
    }
    cout << F[gs][xu] << endl;
  }
  return 0;
}
