#include <bits/stdc++.h>
using namespace std;
int fu[200005], cnt[200005];
int zhao(int x) {
  if (x == fu[x]) return x;
  return fu[x] = zhao(fu[x]);
}
int main() {
  int t, n;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
      fu[i] = i;
      cnt[i] = 1;
    }
    for (int i = 1; i <= n; ++i) {
      int p;
      scanf("%d", &p);
      int j = zhao(i);
      int k = zhao(p);
      if (j != k) {
        fu[j] = k;
        cnt[k] += cnt[j];
      }
    }
    for (int i = 1; i <= n; ++i) {
      printf("%d ", cnt[zhao(i)]);
    }
    printf("\n");
  }
  return 0;
}
