#include <bits/stdc++.h>
using namespace std;
const int N = 20, P2 = 4100;
int n, m, p2, nm;
long long a[N][2005];
long long s[N][P2], ss[P2];
long long f[P2];
int mx[2005], c[2006];
vector<int> e[P2];
bool sort_c(int u, int v) { return mx[u] > mx[v]; }
int MOVE(int first, int second) {
  first += second;
  return first <= n ? first : first - n;
}
void MAIN() {
  n = ({
    long long first = 0;
    int o = 0, c = getchar();
    for (; !isdigit(c); c = getchar()) o = c == '-';
    for (; isdigit(c); c = getchar()) first = first * 10 + c - '0';
    o ? -first : first;
  }),
  m = ({
    long long first = 0;
    int o = 0, c = getchar();
    for (; !isdigit(c); c = getchar()) o = c == '-';
    for (; isdigit(c); c = getchar()) first = first * 10 + c - '0';
    o ? -first : first;
  });
  p2 = (1ll << n) - 1;
  nm = min(n, m);
  for (auto i = 1; i <= n; i++)
    for (auto j = 1; j <= m; j++)
      a[i][j] = ({
        long long first = 0;
        int o = 0, c = getchar();
        for (; !isdigit(c); c = getchar()) o = c == '-';
        for (; isdigit(c); c = getchar()) first = first * 10 + c - '0';
        o ? -first : first;
      });
  for (auto j = 1; j <= m; j++) mx[j] = 0;
  for (auto j = 1; j <= m; j++)
    for (auto i = 1; i <= n; i++)
      ({
        bool o = false;
        if (mx[j] < a[i][j]) {
          mx[j] = a[i][j];
          o = true;
        };
        o;
      });
  for (auto j = 1; j <= m; j++) c[j] = j;
  sort(c + 1, c + m + 1, sort_c);
  for (auto j = 1; j <= nm; j++)
    for (auto tt = 0; tt <= p2; tt++) s[j][tt] = 0;
  for (auto j = 1; j <= nm; j++)
    for (auto mv = 0; mv <= n - 1; mv++) {
      ss[0] = 0;
      for (auto tt = 0; tt <= p2; tt++)
        for (auto b = 1; b <= n; b++)
          if (((tt >> (b - 1)) & 1ll) == 0)
            ss[(tt | (1ll << (b - 1)))] = ss[tt] + a[MOVE(b, mv)][c[j]];
      for (auto tt = 0; tt <= p2; tt++)
        ({
          bool o = false;
          if (s[j][tt] < ss[tt]) {
            s[j][tt] = ss[tt];
            o = true;
          };
          o;
        });
    }
  for (auto tt = 0; tt <= p2; tt++) e[tt].clear();
  for (auto i = 0; i <= p2; i++)
    for (auto j = i + 1; j <= p2; j++)
      if ((i & j) == i) e[i].push_back(j);
  for (auto tt = 0; tt <= p2; tt++) f[tt] = 0;
  for (auto j = 1; j <= nm; j++)
    for (auto tt = p2; 0 <= tt; tt--)
      for (auto& t2 : e[tt])
        ({
          bool o = false;
          if (f[t2] < f[tt] + s[j][tt ^ t2]) {
            f[t2] = f[tt] + s[j][tt ^ t2];
            o = true;
          };
          o;
        });
  long long res = 0;
  for (auto tt = 0; tt <= p2; tt++)
    ({
      bool o = false;
      if (res < f[tt]) {
        res = f[tt];
        o = true;
      };
      o;
    });
  cout << res << "\n";
}
int main() {
  int t = ({
    long long first = 0;
    int o = 0, c = getchar();
    for (; !isdigit(c); c = getchar()) o = c == '-';
    for (; isdigit(c); c = getchar()) first = first * 10 + c - '0';
    o ? -first : first;
  });
  for (auto i = 1; i <= t; i++) MAIN();
}
