#include <bits/stdc++.h>
using namespace std;
int n;
int p[8505];
int wh[8505];
int ans[8505];
int level[8505];
int nxt[9][8505];
int prv[9][8505];
pair<int, int> bros[9][8505];
int svd = 0, lsz, l;
inline void go(int &f) {
  int id = f, t = 0;
  f = nxt[l][f];
  if (wh[f] < wh[id]) id = f, t = 1;
  f = nxt[l][f];
  if (wh[f] < wh[id]) id = f, t = 2;
  f = nxt[l][f];
  if (t == 2) bros[l - 1][id] = {prv[l][prv[l][id]], prv[l][id]};
  if (t == 1) bros[l - 1][id] = {prv[l][id], nxt[l][id]};
  if (t == 0) bros[l - 1][id] = {nxt[l][id], nxt[l][nxt[l][id]]};
  level[id]--, lsz++, nxt[l - 1][svd] = id, nxt[l - 1][id] = n + 1,
                              prv[l - 1][id] = svd;
  svd = id;
}
bool comp(int a, int b) { return wh[a] < wh[b]; }
struct genQ {
  int lvl, pl, pr;
};
vector<genQ> s10;
void solve1() {
  vector<int> pos;
  int pl = s10.back().pl;
  int pr = s10.back().pr;
  int lvl = s10.back().lvl;
  s10.pop_back();
  if (pl != 0) {
    if (bros[lvl][pl].first > pl) pos.push_back(bros[lvl][pl].first);
    if (bros[lvl][pl].second > pl) pos.push_back(bros[lvl][pl].second);
  }
  if (pr != n + 1) {
    if (bros[lvl][pr].first < pr) pos.push_back(bros[lvl][pr].first);
    if (bros[lvl][pr].second < pr) pos.push_back(bros[lvl][pr].second);
  }
  int l = 3 * lvl + 1;
  int t = 0;
  for (int i = 0; i < pos.size(); i++) {
    if (ans[pos[i]] == 0) t = 1;
  }
  if (!t) return;
  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;
  }
}
void solve2(vector<int> pos, int l, int r) {
  int t = 0;
  for (int i = 0; i < pos.size(); i++) {
    if (ans[pos[i]] == 0) t = 1;
  }
  if (!t) return;
  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;
  }
}
void solve(int i, int gl) {
  int LL = prv[gl][p[i]], RR = nxt[gl][p[i]];
  s10.push_back({gl, LL, RR});
  s10.push_back({gl, LL, p[i]});
  s10.push_back({gl, p[i], RR});
  prv[gl][RR] = LL;
  nxt[gl][LL] = RR;
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> p[i];
  for (int i = 1; i <= n; i++)
    wh[p[i]] = i, ans[i] = 0, nxt[8][i] = i + 1, level[i] = 8,
    prv[8][i] = i - 1;
  for (int i = 1; i <= 8; i++) nxt[i][0] = 1, nxt[i][n + 1] = n + 1;
  wh[n + 1] = n + 1;
  if (n > 4)
    for (l = 8; l >= 1; l--) {
      int f = nxt[l][0];
      lsz = 0, svd = 0;
      while (f != n + 1) go(f);
      if (lsz <= 4) break;
    }
  vector<int> calls[8];
  for (int i = n; i >= 1; i--) {
    for (int gl = level[p[i]]; gl < 8; gl++) {
      calls[gl].push_back(i);
    }
  }
  for (int gl = 1; gl != 8; gl++) {
    for (auto i : calls[gl]) solve(i, gl);
    while (s10.size()) solve1();
  }
  for (int lvl = 1; lvl <= 8; lvl++) {
    vector<int> ao;
    for (int i = 1; i <= n; i++)
      if (lvl == level[i] && ans[i] == 0) ao.push_back(i);
    solve2(ao, lvl * 3 - 2, lvl * 3);
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
  cout << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
