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;
class InputReader {
 public:
  InputReader() {
    input_file = stdin;
    cursor = 0;
    fread(buffer, SIZE, 1, input_file);
  }
  InputReader(const char *file_name) {
    input_file = fopen(file_name, "r");
    cursor = 0;
    fread(buffer, SIZE, 1, input_file);
  }
  inline InputReader &operator>>(int &n) {
    while (buffer[cursor] < '0' || buffer[cursor] > '9') {
      advance();
    }
    n = 0;
    while ('0' <= buffer[cursor] && buffer[cursor] <= '9') {
      n = n * 10 + buffer[cursor] - '0';
      advance();
    }
    return *this;
  }

 private:
  FILE *input_file;
  static const int SIZE = 1 << 17;
  int cursor;
  char buffer[SIZE];
  inline void advance() {
    ++cursor;
    if (cursor == SIZE) {
      cursor = 0;
      fread(buffer, SIZE, 1, input_file);
    }
  }
};
const int NMAX = 605;
const int KMAX = 17;
int h, w, k;
int img[NMAX][NMAX];
InputReader fin;
void read_img() {
  fin >> h >> w >> k;
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j) fin >> img[i][j];
}
int s_part[NMAX][NMAX];
void compute_s_part() {
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j)
      s_part[i][j] = img[i][j] + s_part[i][j - 1] + s_part[i - 1][j] -
                     s_part[i - 1][j - 1];
}
int average(int l1, int c1, int l2, int c2) {
  int sum = s_part[l2][c2] - s_part[l2][c1 - 1] - s_part[l1 - 1][c2] +
            s_part[l1 - 1][c1 - 1];
  int area = (l2 - l1 + 1) * (c2 - c1 + 1);
  return sum / area;
}
int new_img[NMAX][NMAX];
void blur() {
  int lat = 2;
  for (int i = 1; i <= k; ++i) {
    int u = (i - 1) * (h / k) + 1;
    int d = i * (h / k);
    for (int lin = u; lin <= d; ++lin)
      for (int col = 1; col <= w; ++col)
        new_img[lin][col] = average(max(u, lin - lat), max(1, col - lat),
                                    min(d, lin + lat), min(w, col + lat));
  }
}
int cost[KMAX][KMAX];
int compute_cost(int u1, int d1, int u2, int d2) {
  int ans = 0;
  for (int col = 1; col <= w; ++col)
    ans += (new_img[d1][col] - new_img[u2][col]) *
           (new_img[d1][col] - new_img[u2][col]);
  return ans;
}
const int INF = 1e9 + 15;
int dp[1 << KMAX][KMAX];
int father[1 << KMAX][KMAX];
int compute_dp() {
  for (int i = 0; i < (1 << k); ++i)
    for (int j = 0; j < k; ++j) dp[i][j] = INF;
  for (int i = 0; i < k; ++i) dp[1 << i][i] = 0;
  for (int i = 0; i < (1 << k); ++i)
    for (int j = 0; j < k; ++j)
      if ((i & (1 << j)) && dp[i][j] < INF)
        for (int l = 0; l < k; ++l)
          if (!(i & (1 << l)))
            if (dp[i][j] + cost[j + 1][l + 1] < dp[i | (1 << l)][l]) {
              dp[i | (1 << l)][l] = dp[i][j] + cost[j + 1][l + 1];
              father[i | (1 << l)][l] = j;
            }
  int ans = INF;
  int node;
  for (int j = 0; j < k; ++j)
    if (dp[(1 << k) - 1][j] < ans) {
      ans = dp[(1 << k) - 1][j];
      node = j + 1;
    }
  return node;
}
int p[KMAX];
int p_inv[KMAX];
void solve() {
  int state = (1 << k) - 1;
  int node = compute_dp();
  int pos = 0;
  p[++pos] = node;
  while (state & (state - 1)) {
    p[++pos] = father[state][node - 1] + 1;
    state ^= (1 << (node - 1));
    node = p[pos];
  }
  reverse(p + 1, p + k + 1);
  for (int i = 1; i <= k; ++i) p_inv[p[i]] = i;
  for (int i = 1; i <= k; ++i) cout << p_inv[i] << " \n"[i == k];
}
int main() {
  int q = 0;
  fin >> q;
  while (q--) {
    read_img();
    compute_s_part();
    blur();
    for (int i = 1; i <= k; ++i) {
      int ui = (i - 1) * (h / k) + 1;
      int di = i * (h / k);
      for (int j = 1; j <= k; ++j) {
        int uj = (j - 1) * (h / k) + 1;
        int dj = j * (h / k);
        cost[i][j] = compute_cost(ui, di, uj, dj);
      }
    }
    solve();
  }
  return 0;
}