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>
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
using namespace std;
inline void INPUT(const string &s) { freopen((s).c_str(), "r", stdin); }
inline void OUTPUT(const string &s) { freopen((s).c_str(), "w", stdout); }
int q, ar[605][605];
int pal[][100] = {{2, 1},
                  {2, 3, 1},
                  {4, 3, 2, 1},
                  {1, 3, 4, 2, 5},
                  {1, 6, 11, 3, 10, 9, 15, 12, 7, 13, 2, 5, 4, 14, 8},
                  {5, 1, 3, 11, 10, 7, 6, 9, 13, 15, 12, 4, 14, 2, 8},
                  {9, 7, 2, 5, 1, 10, 8, 3, 4, 6},
                  {2, 1, 4, 3},
                  {4, 12, 8, 2, 9, 14, 5, 7, 1, 6, 10, 13, 15, 3, 11},
                  {11, 5, 6, 8, 10, 12, 7, 2, 1, 4, 9, 3, 13, 14, 15},
                  {11, 7, 8, 4, 5, 15, 13, 14, 3, 9, 12, 2, 1, 10, 6},
                  {12, 7, 11, 4, 10, 2, 5, 14, 13, 1, 6, 3, 9, 8, 15, 16},
                  {3, 2, 1},
                  {4, 2, 1, 3, 5},
                  {1, 8, 11, 15, 3, 2, 7, 16, 13, 4, 6, 10, 9, 12, 5, 14},
                  {9, 8, 6, 13, 11, 10, 2, 7, 14, 12, 5, 4, 15, 3, 1},
                  {2, 15, 16, 10, 8, 5, 9, 3, 11, 7, 1, 14, 12, 4, 6, 13},
                  {15, 5, 2, 14, 3, 13, 1, 7, 12, 8, 4, 10, 6, 11, 9},
                  {9, 7, 3, 14, 2, 12, 13, 5, 1, 15, 11, 10, 8, 4, 6}};
int main() {
  int basi = 460;
  srand(time(NULL));
  cin >> q;
  vector<pair<int, int> > anz;
  for (int z = 0; z < q; ++z) {
    int h, w, k;
    cin >> h >> w >> k;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        scanf("%d", &ar[i][j]);
      }
    }
    if (z < 20) {
      for (int i = 0; i < k; i++) {
        printf("%d ", pal[z][i]);
      }
      cout << endl;
      continue;
    }
    int div = h / k;
    int hash1 = 0, hash2 = 0;
    int prime1 = 17;
    int prime2 = 23;
    for (int i = 1; i <= h + 1; i++) {
      if (i % div == 1) {
        anz.push_back({hash1, hash2});
        prime1 = 17;
        prime2 = 23;
        hash1 = 0;
        hash2 = 0;
        if (i == h + 1) break;
      }
      for (int j = 1; j <= w; j++) {
        hash1 += (ar[i][j] * prime1);
        hash2 += (ar[i][j] * prime2);
        prime1 *= 17;
        prime2 *= 23;
      }
    }
    vector<int> sv;
    int idx_bawah = 0, idx_atas = 0;
    vector<vector<int> > urut;
    int correct = 0, wrong = 0;
    map<int, int> ada;
    int num_cor = -1;
    for (int pilih = 1; pilih <= k; pilih++) {
      ada.clear();
      vector<int> sapi;
      ada[pilih] = 1;
      idx_bawah = pilih * div;
      sapi.push_back(pilih);
      int counter = 1, bawahbeh;
      int iter = 0;
      int sum_cor = 0;
      while (1) {
        int ani = -1, pivot, atasbeh;
        bool ok = false;
        for (int now = 1; now <= k; now++) {
          correct = 0;
          if (ada.count(now) != 0) continue;
          int bawahnya = now * div;
          int banding_atas = (now - 1) * div + 1;
          int banding_atas1 = (now - 1) * div + 2;
          int banding_atas2 = (now - 1) * div + 3;
          int banding_atas3 = (now - 1) * div + 4;
          for (int j = 1; j <= w - 3; j++) {
            int bawah = ar[idx_bawah][j];
            int atas = ar[banding_atas][j];
            int sum_bawah =
                ar[idx_bawah][j] + ar[idx_bawah - 1][j] + ar[idx_bawah - 2][j];
            sum_bawah += ar[idx_bawah][j + 1] + ar[idx_bawah - 1][j + 1] +
                         ar[idx_bawah - 2][j + 1];
            sum_bawah += ar[idx_bawah][j + 2] + ar[idx_bawah - 1][j + 2] +
                         ar[idx_bawah - 2][j + 2];
            sum_bawah += ar[idx_bawah][j + 3] + ar[idx_bawah - 1][j + 3] +
                         ar[idx_bawah - 2][j + 3];
            int sum_atas = ar[banding_atas][j] + ar[banding_atas1][j] +
                           ar[banding_atas2][j];
            sum_atas += ar[banding_atas][j + 1] + ar[banding_atas1][j + 1] +
                        ar[banding_atas2][j + 1];
            sum_atas += ar[banding_atas][j + 2] + ar[banding_atas1][j + 2] +
                        ar[banding_atas2][j + 2];
            sum_atas += ar[banding_atas][j + 3] + ar[banding_atas1][j + 3] +
                        ar[banding_atas2][j + 3];
            int sel = abs(sum_bawah - sum_atas);
            if (sel < basi) {
              correct++;
            }
          }
          if (correct > ani) {
            pivot = now;
            ani = correct;
            bawahbeh = bawahnya;
          }
        }
        sum_cor += ani;
        idx_bawah = bawahbeh;
        sapi.push_back(pivot);
        ada[pivot] = 1;
        counter++;
        if (counter == k) break;
      }
      if (z == 3) {
      }
      if (sum_cor > num_cor) {
        num_cor = sum_cor;
        sv.clear();
        for (int i = 0; i < k; i++) {
          sv.push_back(sapi[i]);
        }
      }
    }
    prime1 = 17;
    prime2 = 23;
    vector<pair<int, int> > realans;
    for (int i = 0; i < sv.size(); i++) {
      hash1 = 0;
      hash2 = 0;
      prime1 = 17;
      prime2 = 23;
      int awal = (sv[i] - 1) * div + 1;
      int akhir = (sv[i] * div);
      for (int j = awal; j <= akhir; j++) {
        for (int k = 1; k <= w; k++) {
          hash1 += (ar[j][k] * prime1);
          hash2 += (ar[j][k] * prime2);
          prime1 *= 17;
          prime2 *= 23;
        }
      }
      realans.push_back({hash1, hash2});
    }
    for (int i = 0; i < anz.size(); i++) {
      for (int j = 0; j < realans.size(); j++) {
        if ((realans[j].first == anz[i].first) &&
            (realans[j].second == anz[i].second)) {
          printf("%d ", j + 1);
          break;
        }
      }
    }
    cout << endl;
  }
  return 0;
}