#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXH = 600;
const int MAXW = 600;
const int MAXK = 16;
int h, w, k;
int orig[MAXH][MAXW];
int g[MAXH][MAXW];
int res[MAXH][MAXW];
long long val[MAXK][MAXK];
long long dp[MAXK + 1][MAXK][1 << MAXK];
int act[MAXK + 1][MAXK][1 << MAXK];
int ret[MAXK];
char fname[100];
int casenr;
FILE *file;
int fsz;
void writechar(int x) {
  ++fsz;
  fputc(x & 255, file);
}
void writeshort(int x) {
  fsz += 2;
  fputc(x & 255, file);
  fputc((x >> 8) & 255, file);
}
void writeint(int x) {
  fsz += 4;
  fputc(x & 255, file);
  fputc((x >> 8) & 255, file);
  fputc((x >> 16) & 255, file);
  fputc((x >> 24) & 255, file);
}
void writebmp() {
  int hsz = 40;
  sprintf(fname, "e2/out%d.bmp", casenr);
  file = fopen(fname, "wb");
  fsz = 0;
  writeshort(19778);
  writeint(14 + hsz + 4 * 256 + h * 4 * ((w + 3) / 4));
  writeshort(0);
  writeshort(0);
  writeint(14 + hsz + 4 * 256 - 1024);
  writeint(hsz);
  writeint(w);
  writeint(h);
  writeshort(1);
  writeshort(8);
  for (int i = (0); i < (6); ++i) writeint(0);
  for (int i = (0); i < (256); ++i) {
    for (int j = (0); j < (3); ++j) writechar(i);
    writechar(i);
  }
  for (int x = (0); x < (h); ++x) {
    int n4 = (w + 3) / 4;
    for (int i = (0); i < (n4 - 1); ++i)
      for (int j = (0); j < (4); ++j)
        writechar(i * 4 + j < w ? res[h - x - 1][i * 4 + j] : 255);
    for (int j = (0); j < (4); ++j)
      writechar((n4 - 1) * 4 + j < w ? res[h - x - 1][(n4 - 1) * 4 + j] : 255);
  }
  for (int i = (0); i < (1000); ++i) writechar(0);
  fclose(file);
}
void run() {
  scanf("%d%d%d", &h, &w, &k);
  for (int x = (0); x < (h); ++x)
    for (int y = (0); y < (w); ++y) scanf("%d", &g[x][y]);
  const int DAVG = 1;
  const int DCMP = 0;
  for (int x = (0); x < (h); ++x)
    for (int y = (0); y < (w); ++y) orig[x][y] = g[x][y];
  for (int x = (0); x < (h); ++x)
    for (int y = (0); y < (w); ++y) {
      int sum = 0, cnt = 0;
      for (int dx = (-DAVG); dx <= (+DAVG); ++dx)
        for (int dy = (-DAVG); dy <= (+DAVG); ++dy) {
          int nx = x + dx, ny = y + dy;
          if (nx < 0 || nx >= h || ny < 0 || ny >= w ||
              x / (h / k) != nx / (h / k))
            continue;
          sum += orig[nx][ny], ++cnt;
        }
      g[x][y] = sum / cnt;
    }
  for (int i = (0); i < (k); ++i)
    for (int j = (0); j < (k); ++j) {
      if (i == j) {
        val[i][j] = LLONG_MAX;
        continue;
      }
      long long cur = 0;
      int x1 = (i + 1) * (h / k) - 1, x2 = j * (h / k);
      for (int y = (0); y < (w); ++y)
        for (int dx = (0); dx <= (+DCMP); ++dx)
          for (int dy = (-DCMP); dy <= (+DCMP); ++dy) {
            int nx1 = x1 - dx, nx2 = x2 + dx, ny = y + dy;
            if (ny < 0 || ny >= w) continue;
            int diff = abs(g[nx1][y] - g[nx2][ny]);
            cur += (long long)diff;
          }
      val[i][j] = cur;
    }
  for (int i = (0); i <= (k); ++i)
    for (int j = (0); j < (k); ++j)
      for (int mask = (0); mask < (1 << k); ++mask) dp[i][j][mask] = LLONG_MAX;
  for (int j = (0); j < (k); ++j) {
    int cmask = (1 << k) - (1 << j) - 1;
    dp[1][j][cmask] = 0;
  }
  for (int i = (0); i < (k); ++i)
    for (int j = (0); j < (k); ++j)
      for (int mask = (0); mask < (1 << k); ++mask)
        if (dp[i][j][mask] != LLONG_MAX) {
          for (int nj = (0); nj < (k); ++nj)
            if (mask & (1 << nj)) {
              long long nval = dp[i][j][mask] + val[j][nj];
              int nmask = mask - (1 << nj);
              if (nval < dp[i + 1][nj][nmask])
                dp[i + 1][nj][nmask] = nval, act[i + 1][nj][nmask] = j;
            }
        }
  int bj = 0, bmask = 0;
  for (int j = (1); j < (k); ++j)
    if (dp[k][j][bmask] < dp[k][bj][bmask]) bj = j;
  for (int i = k; i >= 1; --i) {
    ret[bj] = i - 1;
    int nj = act[i][bj][bmask];
    bmask += (1 << bj);
    bj = nj;
  }
  assert(bmask == (1 << k) - 1);
  for (int i = (0); i < (k); ++i) {
    if (i != 0) printf(" ");
    printf("%d", ret[i] + 1);
  }
  puts("");
  int dh = h / k;
  for (int i = (0); i < (k); ++i)
    for (int x = (0); x < (dh); ++x)
      for (int y = (0); y < (w); ++y)
        res[ret[i] * dh + x][y] = orig[i * dh + x][y];
}
int main() {
  int n;
  scanf("%d", &n);
  for (casenr = 1; casenr <= n; ++casenr) run();
  return 0;
}
