#include <bits/stdc++.h>
using namespace std;
int h, w, K;
int a[600][600];
int b[600][600];
template <class T1, class T2>
bool chmin(T1 &a, T2 b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2>
bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
void mean(int y1, int y2) {
  for (int i = y1; i < y2; i++) {
    for (int j = 0; j < w; j++) {
      double s = 0;
      int num = 0;
      for (int dy = -2; dy <= 2; dy++) {
        for (int dx = -2; dx <= 2; dx++) {
          int ny = i + dy;
          int nx = j + dx;
          if (ny < y1 || nx < 0 || ny >= y2 || nx >= w) continue;
          s += a[ny][nx];
          num++;
        }
      }
      s /= num;
      b[i][j] = s;
    }
  }
}
int ok(int k1, int k2) {
  int y1 = h / K * (k1 + 1) - 1;
  int y2 = h / K * k2;
  int a[] = {1000, 500, 250, 120, 60, 30, 15, 7, 3, 1};
  int num = 0;
  for (int k = 0; k < 10; k++) {
    for (int j = 0; j < w; j++) {
      if (abs(b[y1][j - k] - b[y2][j + k]) <= 10) num += a[k];
    }
  }
  return num;
}
double ok2(int k1, int k2) {
  int y1 = h / K * (k1 + 1) - 1;
  int y2 = h / K * k2;
  const int M = 5;
  double s[M] = {};
  double c[] = {100, 50, 25, 10, 5, 2, 1, 0.5, 0.25, 0.10, 0.05};
  double res = 0;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < w; j++) {
      s[i] += pow(b[y1 - i][j] - b[y2 + i][j], 2);
    }
    res += sqrt(s[i]) * c[i];
  }
  return res;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T x : v) os << x << " ";
  return os;
}
void solve() {
  cin >> h >> w >> K;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  for (int i = 0; i < K; i++) mean(h / K * i, h / K * (i + 1));
  int cost[16][16] = {};
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++)
      if (i != j) {
        cost[i][j] = ok2(i, j);
      }
  }
  static int prev[1 << 16][16];
  static long long dp[1 << 16][16];
  const long long inf = 1e15;
  memset(prev, -1, sizeof(prev));
  fill_n(*dp, (1 << 16) * 16, inf);
  for (int i = 0; i < K; i++) dp[1 << i][i] = 0;
  for (int i = 1; i < 1 << K; i++) {
    for (int j = 0; j < K; j++) {
      if (dp[i][j] == inf) continue;
      for (int k = 0; k < K; k++) {
        if (~i & 1 << k) {
          if (chmin(dp[i | 1 << k][k], dp[i][j] + cost[j][k])) {
            prev[i | 1 << k][k] = j;
          }
        }
      }
    }
  }
  int s = (1 << K) - 1;
  int c = min_element(dp[s], dp[s] + K) - dp[s];
  vector<int> p;
  while (c != -1) {
    p.push_back(c);
    int ns = s ^ 1 << c;
    c = prev[s][c];
    s = ns;
  }
  reverse(p.begin(), p.end());
  vector<int> inv(K);
  for (int i = 0; i < K; i++) inv[p[i]] = i + 1;
  cout << inv << endl;
}
int main() {
  int q;
  cin >> q;
  while (q--) solve();
}
