#include <bits/stdc++.h>
using namespace std;
const long double EPS = (long double)1e-7;
const long double PI = acos(0) * 2;
bool isZero(const long double& x) { return abs(x) <= EPS; }
int sign(const long double& x) { return isZero(x) ? 0 : (0 < x ? 1 : -1); }
long long gcd(long long a, long long b) {
  for (; b; a %= b, swap(a, b)) {
  }
  return abs(a);
}
pair<long long, long long> operator+(const pair<long long, long long>& a,
                                     const pair<long long, long long>& b) {
  return pair<long long, long long>(a.first + b.first, a.second + b.second);
}
pair<long long, long long> operator-(const pair<long long, long long>& a,
                                     const pair<long long, long long>& b) {
  return pair<long long, long long>(a.first - b.first, a.second - b.second);
}
pair<long long, long long> operator*(const pair<long long, long long>& a,
                                     const long long& b) {
  return pair<long long, long long>(a.first * b, a.second * b);
}
long long operator*(const pair<long long, long long>& a,
                    const pair<long long, long long>& b) {
  return a.first * b.second - b.first * a.second;
}
long long ccw(const pair<long long, long long>& a,
              const pair<long long, long long>& b,
              const pair<long long, long long>& c) {
  return a * b + b * c + c * a;
}
void fg(vector<int> G[], int a, int b) {
  G[a].emplace_back(b);
  G[b].emplace_back(a);
}
void fg(vector<pair<int, int> > G[], int a, int b, int c) {
  G[a].emplace_back(b, c);
  G[b].emplace_back(a, c);
}
int dp[1 << 12], C[1 << 12];
vector<int> A[2005];
int B[2005][12];
int T, H, W;
void run() {
  cin >> H >> W;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) cin >> B[j][i];
  for (int i = 0; i < W; i++) {
    rotate(B[i], max_element(B[i], B[i] + H), B[i] + H);
    A[i].clear();
    for (int j = 0; j < H; j++) A[i].emplace_back(B[i][j]);
  }
  sort(A, A + W);
  reverse(A, A + W);
  memset(dp, 0, (1 << H) * 4);
  for (int i = 0; i < min(H, W); i++) {
    memset(C, 0, (1 << H) * 4);
    for (int key = 1 << H; key--;) {
      int sum = 0;
      for (int j = 0; j < H; j++)
        if (key & (1 << j)) sum += A[i][j];
      C[key] = sum;
    }
    for (int key = 1 << H; key--;) {
      vector<int> V;
      V.emplace_back(key);
      for (int j = 0, nkey = key; j < H; j++) {
        nkey <<= 1;
        if (nkey & (1 << H)) {
          nkey ^= 1 << H;
          nkey |= 1;
        }
        V.emplace_back(nkey);
      }
      int mx = 0;
      for (int v : V) (mx) = max((mx), (C[v]));
      for (int v : V) C[v] = mx;
    }
    for (int key = 1 << H; key--;) {
      (dp[key]) = max((dp[key]), (C[key]));
      for (int i = key; i; i = key & (i - 1))
        (dp[key]) = max((dp[key]), (C[key ^ i] + dp[i]));
    }
  }
  cout << dp[(1 << H) - 1] << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> T;
  for (; T--;) run();
  return 0;
}
