Problem: Zombies seem to have become much more intelligent lately – a few have somehow wandered into the base through the automatic gate. Heidi has had to beef up security, and a new gate has been installed. Unfortunately, now the questions being asked are more complicated, and even humans have trouble answering them. Can you still program the robot army to do this reliably?

The new questions are of the following form: a grayscale photograph has been divided into several horizontal pieces, which have been arbitrarily rearranged. The task is to assemble the original image back from these pieces (somewhat like in a jigsaw puzzle). To further delay the zombies, significant Gaussian-distributed noise has been added to the image.

Input

The input format is the same as in the previous version, except that the first line of every question now contains three space-separated numbers h, w and k (1 ≤ h, w ≤ 600, 2 ≤ k ≤ 16) – the height (number of rows) and width (number of columns) of the photograph and the number of pieces, respectively. The number of pieces evenly divides the height, and each piece is of the same height h / k.

Again, there is only one input file to be processed, and the same resources are provided to you as in the previous version (except that now you are given all input images in .bmp format, rather than the first 50).

Output

Your program should print q lines. The i-th line should contain your answer for the i-th question: a space-separated sequence of k numbers π1, π2, ..., πk such that: 

  * π is a permutation of {1, 2, ..., k}, that is, each number from 1 to k appears exactly once in π, 
  * for each j = 1, ..., k, πj is the position (index), in the original image, of the piece which is at position j in the input image. (See the illustration below for clarity.) 

<image>

The second image from the test set. If the three pieces in the original image are numbered 1, 2, 3 from top to bottom, then the numbering in the image on the right should be 2, 3, 1. The correct answer for this image is thus 2 3 1.

Again, your answers will be accepted if they conform to this format and if at least 75% of them are correct.

Again, you may process the input locally and submit just your precomputed answers (i.e., a program which just prints your output for the input file all.in).

Note

The link to download all the necessary materials is http://assets.codeforces.com/files/690/medium_contestant_package.zip

Solution (in CPP):

Solution:
#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;
}