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 int DEBUG = 0;
namespace defs {
template <class Type>
Type read() {
  Type aa;
  int bb = 1, ch;
  while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
    ;
  ch == '-' ? aa = 0, bb = -1 : aa = ch - '0';
  while (ch = getchar(), ch >= '0' && ch <= '9') aa = aa * 10 + ch - '0';
  return bb == 1 ? aa : -aa;
}
const unsigned int rand_x = 11309;
const unsigned int rand_y = 1000000019;
unsigned int rand_cur;
unsigned int rand_int() { return rand_cur = rand_cur * rand_x + rand_y; }
const int inf = 1071026353;
const int mod = 1000000007;
const int modx = 998244353;
}  // namespace defs
namespace lightningwork {
struct pair {
  int u, v;
  long double w;
  bool operator<(const pair &p) const { return w < p.w; }
} p[1000];
struct picture {
  double key[300][400];
} pic[20];
int (*read)() = defs::read<int>;
double map[300][400];
double tim[8][8] = {{0.007, 0.009, 0.013, 0.017, 0.013, 0.009, 0.007},
                    {0.009, 0.013, 0.017, 0.025, 0.017, 0.013, 0.009},
                    {0.013, 0.017, 0.025, 0.035, 0.025, 0.017, 0.013},
                    {0.017, 0.025, 0.035, 0.050, 0.035, 0.025, 0.017},
                    {0.013, 0.017, 0.025, 0.035, 0.025, 0.017, 0.013},
                    {0.009, 0.013, 0.017, 0.025, 0.017, 0.013, 0.009},
                    {0.007, 0.009, 0.013, 0.017, 0.013, 0.009, 0.007}};
int n, m, k, _n;
double f[66666][20], mer[20][20];
int ans[20], from[66666][20];
double merge(int u, int v) {
  double ret = 0;
  for (int i = 1; i <= _n; ++i) {
    for (int j = 1; j <= m; ++j) {
      map[i][j] = pic[u].key[i][j];
    }
  }
  for (int i = 1; i <= _n; ++i) {
    for (int j = 1; j <= m; ++j) {
      map[i + _n][j] = pic[v].key[i][j];
    }
  }
  for (int j = 1; j <= m; ++j) {
    double t1 = 0, t2 = 0;
    for (int x = -3; x <= 3; ++x) {
      for (int y = -3; y <= 3; ++y) {
        if (j + y < 1 || j + y > m) continue;
        t1 += tim[x + 3][y + 3] * tim[x + 3][y + 3] * map[_n + x][j + y];
        t2 += tim[x + 3][y + 3] * tim[x + 3][y + 3] * map[_n + x + 1][j + y];
      }
    }
    double tmp = t1 - t2;
    tmp < 0 ? tmp = -tmp : 0;
    tmp = pow(tmp, 0.3);
    int t = ((j) < (m + 1 - j) ? (j) : (m + 1 - j));
    ret += tmp * pow(t + 100, 0.3);
  }
  return ret;
}
void solve() {
  memset(map, 0, sizeof map);
  n = read();
  m = read();
  k = read();
  _n = n / k;
  for (int _i = 1; _i <= k; ++_i) {
    for (int i = 1; i <= _n; ++i) {
      for (int j = 1; j <= m; ++j) {
        pic[_i].key[i][j] = read() + 100;
      }
    }
  }
  int full = (1 << k) - 1;
  for (int i = 0; i <= full; ++i) {
    for (int j = 0; j <= k; ++j) {
      f[i][j] = 1e50;
    }
  }
  for (int i = 1; i <= k; ++i) {
    f[1 << (i - 1)][i] = 0;
    for (int j = 1; j <= k; ++j) {
      mer[i][j] = merge(i, j);
    }
  }
  for (int i = 1; i <= full; ++i) {
    for (int j = 1; j <= k; ++j) {
      if (i & (1 << (j - 1))) {
        for (int l = 1; l <= k; ++l) {
          if (i & (1 << (l - 1))) continue;
          double tmp = f[i][j] + mer[j][l];
          if (tmp <= f[i | (1 << (l - 1))][l]) {
            f[i | (1 << (l - 1))][l] = tmp;
            from[i | (1 << (l - 1))][l] = j;
          }
        }
      }
    }
  }
  int end = 0;
  for (int i = 1; i <= k; ++i) {
    if (f[full][i] < f[full][end]) end = i;
  }
  for (int i = 1; i <= k; ++i) {
    ans[end] = k + 1 - i;
    if (i < k) {
      int sta = full;
      full ^= 1 << (end - 1);
      end = from[sta][end];
    }
  }
  for (int i = 1; i <= k; ++i) {
    printf("%d ", ans[i]);
  }
  puts("");
}
void work() {
  int t = read();
  while (t--) {
    solve();
  }
}
}  // namespace lightningwork
int main() {
  if (0)
    freopen(
        "work"
        ".in",
        "r", stdin),
        freopen(
            "work"
            ".out",
            "w", stdout);
  lightningwork::work();
  if (0) fclose(stdin), fclose(stdout);
  return 0;
}