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;
const long double EPS = 0.000001;
const int MAXN = 605;
const int MAXK = 16;
const long long int INF = 1000000000000000LL;
const int DX = 2;
const int DY = 1;
int h, w, k, ph, img[MAXN][MAXN];
long long int graph[MAXN][MAXN];
int parent[MAXK][1 << MAXK];
long long int dp[MAXK][1 << MAXK];
int where[MAXK];
long long int ConnectionQuality(int a, int b) {
  long long int result = 0;
  int ra = ph * (a + 1) - 1;
  int rb = ph * b;
  for (int i = 0; i < w; i++) {
    long long int r = 0;
    for (int x1 = -DX; x1 <= DX; x1++)
      for (int y1 = -DY; y1 <= 0; y1++)
        for (int x2 = -DX; x2 <= DX; x2++)
          for (int y2 = 0; y2 <= DY; y2++) {
            int ax = i + x1;
            int bx = i + x2;
            int ay = ra + y1;
            int by = rb + y2;
            if ((ax < 0) || (ax >= w) || (ay < 0) || (ay >= h)) continue;
            long long int diff = max(0, abs(img[ay][ax] - img[by][bx]) - 32);
            r += diff * diff;
          }
    result += r;
  }
  return result;
}
long long int Go(int vertex, int visited) {
  if (dp[vertex][visited] != INF) return dp[vertex][visited];
  if (visited == (1 << vertex)) return 0LL;
  assert(visited & (1 << vertex));
  long long int result = INF;
  for (int i = 0; i < k; i++)
    if ((i != vertex) && ((visited & (1 << i)) != 0)) {
      long long int res = Go(i, visited ^ (1 << vertex)) + graph[i][vertex];
      if (res < result) {
        parent[vertex][visited] = i;
        result = res;
      }
    }
  dp[vertex][visited] = result;
  return result;
}
vector<int> TSP() {
  for (int i = 0; i < k; i++)
    for (int j = 0; j < (1 << k); j++) {
      dp[i][j] = INF;
      parent[i][j] = -1;
    }
  int full_mask = (1 << k) - 1;
  long long int result = INF, best_finish = -1;
  for (int i = 0; i < k; i++) {
    long long int res = Go(i, full_mask);
    if (res < result) {
      result = res;
      best_finish = i;
    }
  }
  int u = best_finish;
  int v = full_mask;
  vector<int> path;
  path.push_back(u);
  for (int i = 0; i < k - 1; i++) {
    int p = u;
    u = parent[u][v];
    v ^= (1 << p);
    path.push_back(u);
  }
  reverse(path.begin(), path.end());
  return path;
}
int main(int argc, char* argv[]) {
  FILE* f_correct;
  if (argc > 1) f_correct = fopen(argv[1], "r");
  int cases_correct = 0;
  int q;
  scanf("%d", &q);
  while (q--) {
    scanf("%d%d%d", &h, &w, &k);
    fprintf(stderr, "k = %d\n", k);
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) scanf("%d", &img[i][j]);
    ph = h / k;
    for (int i = 0; i < k; i++)
      for (int j = 0; j < k; j++) {
        if (i == j) continue;
        graph[i][j] = ConnectionQuality(i, j);
      }
    vector<int> solution = TSP();
    for (int i = 0; i < k; i++) where[solution[i]] = i;
    int ok = 0;
    for (int i = 0; i < k; i++) {
      printf("%d ", where[i] + 1);
      if (argc > 1) {
        int num;
        fscanf(f_correct, "%d", &num);
        if (num == where[i] + 1) ok++;
      }
    }
    printf("\n");
    if (argc > 1) {
      fprintf(stderr, "%s %2d/%2d\n\n", ok == k ? " OK " : "FAIL", ok, k);
      if (ok == k) cases_correct++;
    }
  }
  if (argc > 1) {
    fclose(f_correct);
    fprintf(stderr, "\n==> %2d/%2d\n", cases_correct, 20);
  }
  return 0;
}