#include <bits/stdc++.h>
int cal_submask(int current, int mask,
                const std::vector<std::vector<int>>& matrix,
                std::vector<std::vector<int>>& memo) {
  if (mask == 0) return 0;
  if (memo[current][mask] >= 0) return memo[current][mask];
  if (current == 0) {
    int temp = 0;
    for (auto i = 0; i < matrix[current].size(); ++i) {
      if (mask % 2 == 1) {
        temp += matrix[current][i];
      }
      mask /= 2;
    }
    return memo[current][mask] = temp;
  }
  if (mask % 2 == 0) {
    return memo[current][mask] = cal_submask(current, mask / 2, matrix, memo);
  }
  int max = 0;
  for (auto i = 0; i < matrix[current].size(); ++i) {
    int temp = 0;
    int m = mask;
    for (auto j = 0; j < matrix[current].size(); ++j) {
      if ((m & 1) != 0) {
        temp += matrix[current][(i + j) % matrix[current].size()];
      }
      m >>= 1;
    }
    max = std::max(max, temp);
  }
  return memo[current][mask] = max;
}
int max_masked(const int current, const int mask,
               const std::vector<std::vector<int>>& matrix,
               std::vector<std::vector<int>>& memo_submask,
               std::vector<std::vector<int>>& memo) {
  if (current == memo.size()) return 0;
  if (memo[current][mask] >= 0) return memo[current][mask];
  int max = 0;
  for (auto submask = 0; submask < memo[current].size();
       submask = (submask + mask + 1) & ~mask) {
    max = std::max(max, cal_submask(current, submask, matrix, memo_submask) +
                            max_masked(current + 1, mask | submask, matrix,
                                       memo_submask, memo));
  }
  return memo[current][mask] = max;
}
int main() {
  int t;
  std::cin >> t;
  for (auto _i = 0; _i < t; ++_i) {
    int n, m;
    std::cin >> n >> m;
    std::vector<std::vector<int>> matrix(n, std::vector<int>(m));
    for (auto& row : matrix)
      for (auto& c : row) std::cin >> c;
    std::vector<int> max_element(m, 0), indices(m);
    for (auto i = 0; i < m; ++i) {
      indices[i] = i;
      for (auto j = 0; j < n; ++j)
        max_element[i] = std::max(max_element[i], matrix[j][i]);
    }
    std::sort(indices.begin(), indices.end(),
              [&max_element](const int a, const int b) {
                return max_element[a] > max_element[b];
              });
    std::vector<std::vector<int>> mat(std::min(n, m));
    for (auto i = 0; i < mat.size(); ++i) {
      for (auto j = 0; j < n; ++j) {
        mat[i].push_back(matrix[j][indices[i]]);
      }
    }
    std::vector<std::vector<int>> memo(
        mat.size(), std::vector<int>(1 << mat.front().size(), -1));
    std::vector<std::vector<int>> memo_submask(
        mat.size(), std::vector<int>(1 << mat.front().size(), -1));
    std::cout << max_masked(0, 0, mat, memo_submask, memo) << std::endl;
  }
}
