#include <bits/stdc++.h>
std::set<int> _set;
std::set<int>::iterator it;
int read_int() {
  char c = getchar();
  int ans = 0;
  bool neg = false;
  while (!isdigit(c)) neg |= (c == '-'), c = getchar();
  while (isdigit(c)) ans = 10 * ans + c - '0', c = getchar();
  return neg ? -ans : ans;
}
void write_int(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write_int(x / 10);
  putchar(x % 10 + '0');
}
int min(int x, int y) { return x < y ? x : y; }
int max(int x, int y) { return x > y ? x : y; }
void _min(int &x, int y) {
  if (x > y) x = y;
}
void _max(int &x, int y) {
  if (x < y) x = y;
}
std::vector<int> discretization(std::vector<int> X) {
  std::vector<int> Y(*std::max_element(X.begin(), X.end()) + 1);
  for (int i = 0; i < X.size(); i++) Y[X[i]] = i + 1;
  for (int i = 0, j = 0; i < Y.size(); i++)
    if (Y[i]) X[Y[i] - 1] = j++;
  return X;
}
std::vector<int> solve(std::vector<int> X) {
  if (X.size() == 1) return {1};
  X = discretization(X);
  std::vector<int> ans(X.size()), nxt;
  std::vector<std::vector<int> > ban(X.size());
  _set.clear();
  for (int i = 0; i < X.size(); i++) {
    it = _set.insert(X[i]).first;
    if (it != _set.begin()) ban[i].push_back(*std::prev(it));
    if (std::next(it) != _set.end()) ban[i].push_back(*std::next(it));
  }
  for (int i = X.size() - 1; i >= 0; i--)
    if (_set.find(X[i]) != _set.end()) {
      ans[X[i]] = 1;
      for (int j : ban[i]) _set.erase(j);
    }
  for (int i = 0; i < X.size(); i++)
    if (ans[X[i]] == 0) nxt.push_back(X[i]);
  if (!nxt.empty()) {
    std::vector<int> nxt_ans = solve(nxt);
    for (int i = 0, j = 0; i < X.size(); i++)
      if (!ans[i]) ans[i] = nxt_ans[j++] + 1;
  }
  return ans;
}
int main() {
  int t = read_int();
  while (t--) {
    std::vector<int> P(read_int());
    for (int i = 0; i < P.size(); i++) P[i] = read_int() - 1;
    std::vector<int> ans = solve(P);
    for (int i = 0; i < ans.size(); i++)
      write_int(ans[i]), putchar(i + 1 == ans.size() ? '\n' : ' ');
  }
  return 0;
}
