#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
int t, n;
int a[maxn];
int father[maxn];
int num[maxn];
int edge[maxn];
int findfather(int x) {
  int a = x;
  while (x != father[x]) x = father[x];
  while (a != father[a]) {
    int z = a;
    a = father[a];
    father[z] = x;
  }
  return x;
}
void Union(int a, int b) {
  int faA = findfather(a);
  int faB = findfather(b);
  if (faA == faB) return;
  father[faA] = faB;
  num[faB] += num[faA];
}
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]), father[i] = i, num[i] = 1;
    for (int i = 1; i <= n; i++) {
      Union(i, a[i]);
    }
    for (int i = 1; i <= n; i++) {
      int tt = findfather(i);
      printf("%d ", num[tt]);
    }
    printf("\n");
  }
}
