#include <bits/stdc++.h>
using namespace std;
int const N = 200009;
int parent[N], sz[N];
void make_set(int id) {
  parent[id] = id;
  sz[id] = 1;
}
int find_set(int id) {
  if (id == parent[id])
    return id;
  else
    return parent[id] = find_set(parent[id]);
}
void merge_set(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (sz[b] > sz[a]) swap(a, b);
    sz[a] += sz[b];
    parent[b] = a;
  }
}
int main() {
  clock_t begin = clock();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) make_set(i);
    for (int i = 1; i <= n; i++) {
      int p;
      cin >> p;
      merge_set(i, p);
    }
    for (int i = 1; i <= n; i++) cout << sz[find_set(i)] << " ";
    cout << '\n';
  }
  return 0;
}
