#include <bits/stdc++.h>
using namespace std;
int dsu[200005];
int size[200005];
int find_set(int cu) {
  if (dsu[cu] == cu) return cu;
  return dsu[cu] = find_set(dsu[cu]);
}
void make_set(int n) {
  for (int i = 1; i <= n; i++) {
    dsu[i] = i;
    size[i] = 1;
  }
}
void union_set(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a == b) {
    return;
  }
  if (size[a] > size[b]) swap(a, b);
  dsu[a] = b;
  size[b] += size[a];
}
int main() {
  int t;
  cin >> t;
  for (int p = 0; p < t; p++) {
    int n;
    cin >> n;
    make_set(n);
    for (int i = 1; i <= n; i++) {
      int k;
      cin >> k;
      union_set(i, k);
    }
    for (int i = 1; i <= n; i++) {
      cout << size[find_set(i)] << " ";
    }
    cout << '\n';
  }
  return 0;
}
