#include <bits/stdc++.h>
using namespace std;
int p[200010];
int a[200010];
int findp(int a) {
  if (p[a] == a) return p[a];
  return p[a] = findp(p[a]);
}
void unionset(int a, int b) {
  int A = findp(a);
  int B = findp(b);
  p[A] = B;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      p[i] = i;
      cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
      unionset(i, a[i]);
    }
    vector<int> ans(n + 1, 0);
    for (int i = 1; i <= n; i++) {
      p[i] = findp(p[i]);
      ans[p[i]]++;
    }
    for (int i = 1; i <= n; i++) {
      cout << ans[p[i]] << " ";
    }
    cout << endl;
  }
}
