#include <bits/stdc++.h>
int inp() {
  char c = getchar();
  int neg = 1;
  while (c < '0' || c > '9') {
    if (c == '-') neg = -1;
    c = getchar();
  }
  int sum = 0;
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum * neg;
}
int f[200][5000], a[20][200], s[20][2010];
int f2[5000], lg[5000], f1[5000];
std::vector<int> id;
bool have[2010], used[2010];
int max[2010];
bool cmp(int x, int y) { return max[x] > max[y]; }
int main() {
  for (int T = inp(); T >= 1; T--) {
    int n = inp();
    int m = inp();
    memset(max, 0, sizeof(max));
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        s[i][j] = inp();
        max[j] = std::max(max[j], s[i][j]);
      }
    id.clear();
    for (int i = 1; i <= m; i++) id.push_back(i);
    std::sort(id.begin(), id.end(), cmp);
    memset(have, false, sizeof(have));
    for (int i = 0; i < std::min(n, m); i++) have[id[i]] = true;
    int mn = m;
    m = 0;
    for (int i = 1; i <= mn; i++) {
      if (!have[i]) continue;
      m++;
      for (int j = 1; j <= n; j++) a[j][m] = s[j][i];
    }
    lg[0] = -1;
    for (int i = 1; i < (1 << n); i++) lg[i] = lg[i >> 1] + 1;
    memset(f, 0, sizeof(f));
    memset(f2, 0, sizeof(f2));
    for (int i = 1; i <= m; i++) {
      memset(f1, 0, sizeof(f1));
      for (int k = 0; k < n; k++) {
        for (int j = 1; j < (1 << n); j++) {
          int l = lg[((j) & (-(j)))] + 1;
          f2[j] = f2[j ^ (1 << (l - 1))] + a[(l + k - 1) % n + 1][i];
          f1[j] = std::max(f1[j], f2[j]);
        }
      }
      for (int j = 0; j < (1 << n); j++) {
        f[i + 1][j] = f[i][j];
        for (int u = j; u; u = j & (u - 1))
          f[i + 1][j] = std::max(f[i + 1][j], f[i][j ^ u] + f1[u]);
      }
    }
    printf("%d\n", f[m + 1][(1 << n) - 1]);
  }
}
