#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q, d = 1, x;
  cin >> n;
  for (int h = 1; h <= n; h++) {
    cin >> q;
    vector<int> pos(200007, 0);
    vector<int> arr = {-1};
    vector<int> day(200007, 0);
    for (int j = 1; j <= q; j++) {
      cin >> x;
      pos[x] = j;
      arr.push_back(x);
    }
    for (int i = 1; i <= q; i++) {
      x = pos[arr[i]];
      d = 1;
      while (x != arr[i]) {
        if (day[pos[pos[arr[i]]]] != 0) {
          d = day[pos[pos[arr[i]]]];
          break;
        }
        if (x < i) {
          d = day[x];
          break;
        }
        x = pos[x];
        d++;
      }
      day[i] = d;
    }
    for (int i = 1; i <= q; i++) {
      cout << day[i] << ' ';
    }
    cout << "\n";
    cout << "\n";
  }
  return 0;
}
