#include <bits/stdc++.h>
using namespace std;
struct DSU {
  long long int parent[500001], connected[500001];
  void intialize(int x) {
    for (long long int i = 1; i <= x; i++) {
      parent[i] = i;
      connected[i] = 1;
    }
  }
  long long int getparent(long long int k) {
    while (k != parent[k]) {
      parent[k] = parent[parent[k]];
      k = parent[k];
    }
    return k;
  }
  long long int getsize(long long int k) { return connected[getparent(k)]; }
  void unionn(long long int u, long long int v) {
    long long int parent1 = getparent(u), parent2 = getparent(v);
    if (parent1 == parent2) return;
    if (connected[parent1] > connected[parent2]) swap(parent1, parent2);
    connected[parent2] += connected[parent1];
    connected[parent1] = 0;
    parent[parent1] = parent[parent2];
  }
};
DSU dsu;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    dsu.intialize(n + 1);
    for (long long int i = 1; i <= n; i++) {
      long long int x;
      cin >> x;
      dsu.unionn(i, x);
    }
    for (long long int i = 1; i <= n; i++) cout << dsu.getsize(i) << " ";
    cout << "\n";
  }
}
