#include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline int read();
struct Comper {
  int val, r, c;
  bool operator<(const Comper& b) const {
    if (val != b.val) return val > b.val;
    if (r != b.r) return r < b.r;
    return c < b.c;
  }
};
int n, m;
int save[16][2048];
int ndcols;
int cpp[16], off[16];
int ans;
void dfs(int c) {
  if (c == ndcols) {
    int tmp = 0;
    for (int i = 0; i < n; ++i) {
      int mx = 0;
      for (int j = 0; j < ndcols; ++j)
        mx = max(mx, save[(i + off[j]) % n][cpp[j]]);
      tmp += mx;
    }
    ans = max(ans, tmp);
  } else
    for (int i = 1; i <= n; ++i) {
      off[c] = i;
      dfs(c + 1);
    }
}
int main(void) {
  int T = read();
  while (T--) {
    n = read(), m = read();
    set<Comper> st;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) {
        save[i][j] = read();
        st.insert({save[i][j], i, j});
      }
    set<int> cols;
    while ((int)cols.size() < min(n, m)) {
      cols.insert(st.begin()->c);
      st.erase(st.begin());
    }
    ans = ndcols = 0;
    for (auto x : cols) cpp[ndcols++] = x;
    dfs(0);
    printf("%d\n", ans);
  }
  return 0;
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
