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;
namespace std {
template <class S, class T>
struct hash<pair<S, T>> {
  size_t operator()(const pair<S, T> &p) const {
    return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
  }
};
template <class T>
struct hash<vector<T>> {
  size_t operator()(const vector<T> &v) const {
    size_t h = 0;
    for (auto i : v) h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
    return h;
  }
};
}  // namespace std
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < (int)v.size(); i++)
    os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{ ";
  for (const auto &i : v) os << i << ", ";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
  os << "{";
  for (const auto &i : v) os << " " << i.first << ": " << i.second << ",";
  return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
  cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
  dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
int dp[1 << 16][16], way[1 << 16][16];
vector<int> tsp(vector<vector<int>> d) {
  int n = d.size();
  for (int i = 0; i < (int)1 << n; i++)
    for (int j = 0; j < (int)n; j++) dp[i][j] = inf;
  for (int i = 0; i < (int)n; i++) dp[0][i] = 0;
  for (int i = 0; i < (int)1 << n; i++)
    for (int j = 0; j < (int)n; j++)
      if (dp[i][j] < inf)
        for (int k = 0; k < (int)n; k++)
          if (j != k && !(i & 1 << k)) {
            if (dp[i | 1 << k][k] <= dp[i][j] + d[j][k]) continue;
            dp[i | 1 << k][k] = dp[i][j] + d[j][k];
            way[i | 1 << k][k] = j;
          }
  int ans = inf, ansi, mask = (1 << n) - 1;
  for (int i = 0; i < (int)n; i++)
    if (ans > dp[mask][i]) ans = dp[mask][ansi = i];
  vector<int> res;
  for (int i = 0; i < (int)n; i++) {
    res.push_back(ansi);
    if (i == n - 1) break;
    int p = way[mask][ansi];
    mask ^= 1 << ansi;
    ansi = p;
  }
  reverse((res).begin(), (res).end());
  return res;
}
int dist(const vector<vector<int>> &v, int x, int y) {
  double res = 0;
  for (int j = 0; j < (int)5; j++) {
    double fac = pow(3.0, -j);
    for (int i = 0; i < (int)v[0].size(); i++)
      res += (v[x - j][i] - v[y][i]) * (v[x - j][i] - v[y][i]) * fac;
    for (int i = 0; i < (int)v[0].size(); i++)
      res += (v[x][i] - v[y + j][i]) * (v[x][i] - v[y + j][i]) * fac;
  }
  return (int)(res / 1000);
}
void filter(vector<vector<int>> &v, int y, int Y) {
  const int X = 3;
  int w = v[0].size();
  vector<vector<int>> u(Y - y, vector<int>(w));
  for (int i = y; i < Y; i++)
    for (int j = 0; j < (int)w; j++) {
      int s = 0, c = 0;
      for (int k = i - X; k <= i + X; k++)
        if (y <= k && k < Y)
          for (int l = j - X; l <= j + X; l++)
            if (0 <= l && l < w) {
              s += v[k][l], c++;
            }
      u[i - y][j] = s / c;
    }
  for (int i = 0; i < (int)Y - y; i++)
    for (int j = 0; j < (int)w; j++) v[i + y][j] = u[i][j];
}
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int q;
  cin >> q;
  vector<int> res;
  for (int it = 0; it < (int)q; it++) {
    int h, w, n;
    cin >> h >> w >> n;
    vector<vector<int>> v(h, vector<int>(w));
    for (int i = 0; i < (int)h; i++)
      for (int j = 0; j < (int)w; j++) cin >> v[i][j];
    for (int i = 0; i < (int)n; i++) filter(v, h / n * i, h / n * (i + 1));
    vector<vector<int>> ds(n, vector<int>(n));
    for (int i = 0; i < (int)n; i++)
      for (int j = 0; j < (int)n; j++)
        if (i != j) {
          int x = (h / n) * (i + 1) - 1;
          int y = (h / n) * j;
          ds[i][j] = dist(v, x, y);
        }
    vector<int> res = tsp(ds);
    vector<int> inv(n);
    for (int i = 0; i < (int)n; i++) inv[res[i]] = i + 1;
    for (int i = 0; i < (int)n; i++)
      printf("%d%c", inv[i], i == n - 1 ? '\n' : ' ');
  }
  return 0;
}