#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e6 + 10;
struct NUM {
  int v;
  int y;
};
bool mycmp(NUM a, NUM b) { return a.v > b.v; };
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<int> > v(n, vector<int>(m));
    vector<NUM> vv(m);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> v[i][j];
      }
    }
    for (int j = 0; j < m; j++) {
      int mm = 0;
      for (int i = 0; i < n; i++) {
        if (mm < v[i][j]) mm = v[i][j];
      }
      vv[j].v = mm;
      vv[j].y = j;
    }
    sort(vv.begin(), vv.end(), mycmp);
    int ret = 0;
    for (int i = 0; i < pow(n, min(m, n)); i++) {
      vector<int> idx(min(m, n));
      int j = i;
      int cnt = 0;
      while (j) {
        idx[cnt++] = j % n;
        j /= n;
      }
      int ans = 0;
      for (int x = 0; x < n; x++) {
        int mm = 0;
        for (int y = 0; y < min(m, n); y++) {
          int value = v[(x + idx[y]) % n][vv[y].y];
          if (mm < value) mm = value;
        }
        ans += mm;
      }
      if (ret < ans) ret = ans;
    }
    cout << ret << endl;
  }
  return 0;
}
