#include <bits/stdc++.h>
using namespace std;
int dp[2][1 << 12], n, m, a[15][2005], gg[1 << 12];
struct node {
  int id, mx;
  bool operator<(const node& b) const { return mx > b.mx; }
} d[2005];
int main() {
  int tt;
  scanf("%d", &tt);
  while (tt--) {
    scanf("%d%d", &n, &m);
    memset(d, 0, sizeof(d));
    memset(dp[0], 0, sizeof(dp[0]));
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        scanf("%d", &a[i][j]);
        d[j].mx = max(d[j].mx, a[i][j]);
        d[j].id = j;
      }
    }
    sort(d + 1, d + m + 1);
    int lim = min(n, m);
    for (int i = 1; i <= lim; ++i) {
      int now = (i & 1);
      int last = (now ^ 1);
      memcpy(dp[now], dp[last], sizeof(dp[last]));
      int x = d[i].id;
      for (int k = 0; k < n; ++k) {
        memcpy(gg, dp[last], sizeof(dp[last]));
        for (int j = 0; j < (1 << n); ++j) {
          for (int l = 1; l <= n; ++l) {
            if (j & (1 << (l - 1))) continue;
            gg[j | (1 << (l - 1))] =
                max(gg[j | (1 << (l - 1))],
                    gg[j] + a[k + l > n ? k + l - n : k + l][x]);
          }
          dp[now][j] = max(gg[j], dp[now][j]);
        }
      }
    }
    printf("%d\n", dp[lim & 1][(1 << n) - 1]);
  }
}
