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;
int h, w, K;
int a[600][600];
int b[600][600];
template <class T1, class T2>
bool chmin(T1 &a, T2 b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2>
bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
void mean(int y1, int y2) {
  for (int i = y1; i < y2; i++) {
    for (int j = 0; j < w; j++) {
      double s = 0;
      int num = 0;
      for (int dy = -2; dy <= 2; dy++) {
        for (int dx = -2; dx <= 2; dx++) {
          int ny = i + dy;
          int nx = j + dx;
          if (ny < y1 || nx < 0 || ny >= y2 || nx >= w) continue;
          s += a[ny][nx];
          num++;
        }
      }
      s /= num;
      b[i][j] = s;
    }
  }
}
int ok(int k1, int k2) {
  int y1 = h / K * (k1 + 1) - 1;
  int y2 = h / K * k2;
  int a[] = {1000, 500, 250, 120, 60, 30, 15, 7, 3, 1};
  int num = 0;
  for (int k = 0; k < 10; k++) {
    for (int j = 0; j < w; j++) {
      if (abs(b[y1][j - k] - b[y2][j + k]) <= 10) num += a[k];
    }
  }
  return num;
}
double ok2(int k1, int k2) {
  int y1 = h / K * (k1 + 1) - 1;
  int y2 = h / K * k2;
  const int M = 5;
  double s[M] = {};
  double c[] = {100, 50, 25, 10, 5, 2, 1, 0.5, 0.25, 0.10, 0.05};
  double res = 0;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < w; j++) {
      s[i] += pow(b[y1 - i][j] - b[y2 + i][j], 2);
    }
    res += sqrt(s[i]) * c[i];
  }
  return res;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T x : v) os << x << " ";
  return os;
}
void solve() {
  cin >> h >> w >> K;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  for (int i = 0; i < K; i++) mean(h / K * i, h / K * (i + 1));
  int cost[16][16] = {};
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++)
      if (i != j) {
        cost[i][j] = ok2(i, j);
      }
  }
  static int prev[1 << 16][16];
  static long long dp[1 << 16][16];
  const long long inf = 1e15;
  memset(prev, -1, sizeof(prev));
  fill_n(*dp, (1 << 16) * 16, inf);
  for (int i = 0; i < K; i++) dp[1 << i][i] = 0;
  for (int i = 1; i < 1 << K; i++) {
    for (int j = 0; j < K; j++) {
      if (dp[i][j] == inf) continue;
      for (int k = 0; k < K; k++) {
        if (~i & 1 << k) {
          if (chmin(dp[i | 1 << k][k], dp[i][j] + cost[j][k])) {
            prev[i | 1 << k][k] = j;
          }
        }
      }
    }
  }
  int s = (1 << K) - 1;
  int c = min_element(dp[s], dp[s] + K) - dp[s];
  vector<int> p;
  while (c != -1) {
    p.push_back(c);
    int ns = s ^ 1 << c;
    c = prev[s][c];
    s = ns;
  }
  reverse(p.begin(), p.end());
  vector<int> inv(K);
  for (int i = 0; i < K; i++) inv[p[i]] = i + 1;
  cout << inv << endl;
}
int main() {
  int q;
  cin >> q;
  while (q--) solve();
}