#include <bits/stdc++.h>
using namespace std;
class Piece {
 public:
  int up[600];
  int down[600];
  vector<int> pcs;
  Piece() {}
} pieces[20];
int x[600][600], w, h, p, u;
void readPhoto() {
  cin >> h >> w >> p;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j) cin >> x[i][j];
  h /= p;
}
void setPieces() {
  int st = 0;
  for (int i = 0; i < p; ++i) {
    pieces[i].pcs.push_back(i + 1);
    for (int j = 0; j < w; j++) {
      pieces[i].up[j] = x[st][j] + x[st + 1][j];
      pieces[i].down[j] = x[st + h - 2][j] + x[st + h - 1][j];
    }
    st += h;
  }
  u = p;
}
bool comp(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) {
  return a.first < b.first;
}
int calcDiff(int a, int b) {
  int diff = 0;
  for (int i = 0; i < w; ++i)
    diff += ((pieces[a].down[i] - pieces[b].up[i]) *
             (pieces[a].down[i] - pieces[b].up[i]));
  return diff;
}
void mrgPieces(int a, int b) {
  pieces[a].pcs.insert(pieces[a].pcs.end(), pieces[b].pcs.begin(),
                       pieces[b].pcs.end());
  swap(pieces[a].down, pieces[b].down);
  swap(pieces[b], pieces[--u]);
}
void cmpPieces() {
  vector<pair<int, pair<int, int> > > v;
  for (int i = 0; i < u; ++i)
    for (int j = 0; j < u; ++j)
      if (i != j) {
        int diff = calcDiff(i, j);
        v.push_back(make_pair(diff, make_pair(i, j)));
      }
  sort(v.begin(), v.end(), comp);
  mrgPieces(v[0].second.first, v[0].second.second);
  v.clear();
}
void cleanPieces() {
  for (int i = 0; i < 20; ++i) pieces[i].pcs.clear();
}
int main() {
  ios_base::sync_with_stdio(false);
  int c;
  cin >> c;
  while (c--) {
    readPhoto();
    setPieces();
    while (u > 1) cmpPieces();
    int tmpPrint[20];
    int spx = 1;
    for (__typeof(pieces[0].pcs.begin()) it = pieces[0].pcs.begin();
         it != pieces[0].pcs.end(); ++it)
      tmpPrint[*it - 1] = spx++;
    for (int i = 0; i < p; ++i) cout << tmpPrint[i] << ' ';
    cout << endl;
    cleanPieces();
  }
}
