#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 solve2(vector<int> pos, int l);
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;
  solve2(pos, l);
}
void solve2(vector<int> pos, int l) {
  if (pos.size() < 4) {
    for (int i = 0; i < pos.size(); i++) {
      if (ans[pos[i]] == 0) {
        for (int j = l; j <= l + 2; j++) {
          int t = 1;
          for (int k = 0; k < pos.size(); k++)
            if (ans[pos[k]] == j) t = 0;
          if (t) {
            ans[pos[i]] = j;
            break;
          }
        }
      }
    }
    return;
  }
  int lst = 0;
  for (int i = 1; i < 4; i++)
    if (wh[pos[i]] > wh[pos[lst]]) lst = i;
  if (ans[pos[lst]] == 0) {
    if (ans[pos[3 - lst]] == 0) {
      if (lst == 0)
        ans[pos[0]] = l + 2, ans[pos[1]] = l, ans[pos[2]] = l + 1,
        ans[pos[3]] = l + 2;
      if (lst == 1)
        ans[pos[0]] = l, ans[pos[1]] = l + 2, ans[pos[2]] = l + 1,
        ans[pos[3]] = l + 2;
      if (lst == 2)
        ans[pos[0]] = l, ans[pos[1]] = l + 1, ans[pos[2]] = l,
        ans[pos[3]] = l + 2;
      if (lst == 3)
        ans[pos[0]] = l, ans[pos[1]] = l + 1, ans[pos[2]] = l + 2,
        ans[pos[3]] = l;
    } else {
      if (lst > 1) {
        lst = 3 - lst;
        reverse((pos).begin(), (pos).end());
      }
      if (lst == 0) {
        ans[pos[1]] = 3 * l + 3 - ans[pos[2]] - ans[pos[3]];
        ans[pos[0]] = ans[pos[1]] + 1;
        if (ans[pos[0]] == l + 3) ans[pos[0]] -= 3;
      }
      if (lst == 1) {
        ans[pos[1]] = ans[pos[3]];
        ans[pos[0]] = 3 * l + 3 - ans[pos[1]] - ans[pos[2]];
      }
    }
    return;
  }
  if (lst > 1) {
    lst = 3 - lst;
    reverse((pos).begin(), (pos).end());
  }
  if (lst == 1) {
    if (wh[pos[2]] < wh[pos[3]] || wh[pos[2]] < wh[pos[0]]) {
      ans[pos[3]] = ans[pos[0]];
      ans[pos[2]] = 3 * l + 3 - ans[pos[0]] - ans[pos[1]];
    } else {
      ans[pos[3]] = ans[pos[1]];
      ans[pos[2]] = 3 * l + 3 - ans[pos[0]] - ans[pos[1]];
    }
  } else {
    ans[pos[2]] = ans[pos[1]] + 1;
    if (ans[pos[2]] == l + 3) ans[pos[2]] -= 3;
    ans[pos[3]] = ans[pos[2]] + 1;
    if (ans[pos[3]] == l + 3) ans[pos[3]] -= 3;
  }
}
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);
  }
  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++) {
      if (r[l] != 0) {
        vector<int> cnt(25);
        int kek = 0;
        cnt[0] = 2;
        for (int rr = l; rr <= n; rr++) {
          cnt[r[rr]]++;
          if (cnt[r[rr]] == 1) kek++;
          if (cnt[r[rr]] == 2) kek--;
          if (kek == 0) {
            for (int i = 1; i <= n; i++) cout << p[i] << " ";
            cout << endl;
            for (int i = 1; i <= n; i++) cout << ans[i] << " ";
            cout << endl;
            cout << "WA " << i << endl;
            exit(0);
          }
        }
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
