#include <bits/stdc++.h>
using namespace std;
int n;
int p[8501];
int wh[8501];
int ans[8501];
bool comp(int a, int b) { return wh[a] < wh[b]; }
void solve(vector<int> pos, int l, int r) {
  if (pos.size() < 4) {
    set<int> allowed;
    for (int i = l; i < l + pos.size(); i++) allowed.insert(i);
    for (int i = 0; i < pos.size(); i++)
      if (ans[pos[i]] != 0) allowed.erase(ans[pos[i]]);
    for (int i = 0; i < pos.size(); i++) {
      if (ans[pos[i]] == 0) {
        ans[pos[i]] = *allowed.begin();
        allowed.erase(allowed.begin());
      }
    }
    return;
  }
  if (pos.size() == 4) {
    int t = 0;
    sort((pos).begin(), (pos).end(), comp);
    for (int lc = l; lc <= l + 2; lc++) {
      vector<int> perm = {l, l + 1, l + 2};
      sort((perm).begin(), (perm).end());
      while (1) {
        int ok = 1;
        for (int i = 0; i < 3; i++) {
          if (ans[pos[i]] != 0 && ans[pos[i]] != perm[i]) {
            ok = 0;
            break;
          }
        }
        if (ans[pos[3]] != 0 && ans[pos[3]] != lc) ok = 0;
        int targ = 0;
        for (int i = 0; i < 3; i++) {
          if (perm[i] == lc) targ = i;
        }
        for (int i = 0; i < 3; i++) {
          if (pos[i] > pos[targ] && pos[i] < pos[3]) {
            targ = -1;
            break;
          }
          if (pos[i] < pos[targ] && pos[i] > pos[3]) {
            targ = -1;
            break;
          }
        }
        if (targ == -1 && ok) {
          for (int i = 0; i < 3; i++) ans[pos[i]] = perm[i];
          ans[pos[3]] = lc;
          t = 1;
          break;
        }
        if (!next_permutation(perm.begin(), perm.end())) break;
      }
      if (t) break;
    }
    return;
  }
  vector<int> keks;
  vector<int> marked(pos.size());
  for (int i = 0; i < pos.size(); i += 3) {
    int mark = i;
    for (int j = 0; j < 3 && i + j < pos.size(); j++)
      if (wh[pos[i + j]] < wh[pos[mark]]) mark = i + j;
    keks.push_back(pos[mark]);
    marked[mark] = 1;
  }
  solve(keks, l, r - 3);
  sort(keks.begin(), keks.end(), comp);
  set<int> table;
  vector<int> linker(n + 1);
  for (int i = 0; i < pos.size(); i++) linker[pos[i]] = i;
  for (int i = 0; i < keks.size(); i++) {
    table.insert(linker[keks[i]]);
    auto l = table.find(linker[keks[i]]);
    if (l != table.begin()) {
      l--;
      vector<int> f;
      for (int j = *l + 1; j % 3 != 0; j++) f.push_back(pos[j]);
      for (int j = (linker[keks[i]] / 3) * 3; j < linker[keks[i]]; j++)
        f.push_back(pos[j]);
      solve(f, r - 2, r);
      l++;
    }
    l++;
    if (l != table.end()) {
      vector<int> f;
      for (int j = linker[keks[i]] + 1; j % 3 != 0; j++) f.push_back(pos[j]);
      for (int j = ((*l) / 3) * 3; j < pos.size() && j < *l; j++)
        f.push_back(pos[j]);
      solve(f, r - 2, r);
    }
  }
  vector<int> pref, suff;
  for (int i = 0; i < pos.size(); i++) {
    if (ans[pos[i]] != 0) break;
    pref.push_back(pos[i]);
  }
  for (int i = pos.size() - 1; i >= 0; i--) {
    if (ans[pos[i]] != 0) break;
    suff.push_back(pos[i]);
  }
  reverse((suff).begin(), (suff).end());
  solve(pref, r - 2, r);
  solve(suff, r - 2, r);
}
void solve() {
  vector<int> pos;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> p[i], wh[p[i]] = i, ans[i] = 0, pos.push_back(i);
  solve(pos, 1, 24);
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
  cout << "\n";
}
void checker() {
  vector<int> r(n + 1);
  for (int i = 1; i <= n; i++) {
    r[p[i]] = ans[p[i]];
    for (int l = 1; l <= n; l++) {
      vector<int> cnt(25);
      cnt[0] = 2;
      int val = 0, summ = 0;
      for (int rr = l; rr <= n; rr++) {
        cnt[r[rr]]++;
        summ += r[rr] != 0;
        if (cnt[r[rr]] == 2) val--;
        if (cnt[r[rr]] == 1) val++;
        if (val == 0 && summ) {
          cout << "WA step : " << i << " " << l << " " << rr << endl;
          exit(0);
        }
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
