#include <bits/stdc++.h>
using namespace std;
using namespace std;
vector<int> a(200002);
void eee(int l, int i, int k, vector<int> &c) {
  if (a[i] == k) {
    c[i] = l;
    return;
  }
  eee(l + 1, a[i], k, c);
  c[i] = c[a[i]];
}
int main() {
  int t, m, n;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<int> c(n + 1, -1);
    for (int i = 0; i < n; i++) {
      cin >> a[i + 1];
    }
    for (int i = 1; i <= n; i++) {
      if (c[i] == -1) {
        eee(1, i, i, c);
      }
    }
    for (int i = 1; i <= n; i++) cout << c[i] << " ";
    cout << endl;
  }
}
