#include <bits/stdc++.h>
using namespace std;
int fa[(100020)], sz[(100020)];
int getfa(int x) {
  if (fa[x] == x) return x;
  return fa[x] = getfa(fa[x]);
}
int n;
int main() {
  int T;
  scanf("%d", &T);
  for (int t = 1; t <= T; t++) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) fa[i] = i, sz[i] = 1;
    for (int i = 1; i <= n; i++) {
      int x;
      scanf("%d", &x);
      int f1 = getfa(i), f2 = getfa(x);
      if (f1 == f2) continue;
      sz[f1] += sz[f2];
      fa[f2] = f1;
    }
    for (int i = 1; i <= n; i++) printf("%d ", sz[getfa(i)]);
    puts("");
  }
  return 0;
}
