#include <bits/stdc++.h>
using namespace std;
vector<int> par;
vector<int> chain;
int find_par(int x) {
  if (x != par[x]) par[x] = find_par(par[x]);
  return par[x];
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    par.clear();
    chain.clear();
    par.resize(n + 1);
    chain.resize(n + 1, 1);
    for (int i = 1; i <= n; i++) par[i] = i;
    for (int i = 1; i <= n; i++) {
      int parent;
      cin >> parent;
      int a = find_par(i);
      int b = find_par(parent);
      if (a != b) {
        par[a] = b;
        chain[b] += chain[a];
        chain[a] = 0;
      }
    }
    for (int i = 1; i <= n; i++) cout << chain[find_par(i)] << " ";
    cout << "\n";
  }
  return 0;
}
