#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const long long int M = 1e9 + 7, maxn = 2e5 + 12;
long long int ans, r, n, m, x, a, y, q, par[maxn];
long long int root(long long int v) {
  return (par[v] < 0 ? v : par[v] = root(par[v]));
}
void mrg(long long int v, long long int b) {
  v = root(v);
  b = root(b);
  if (v == b) return;
  par[v] += par[b];
  par[b] = v;
}
int32_t main() {
  cin >> q;
  while (q--) {
    cin >> n;
    long long int a[n + 1];
    fill(par, par + n + 1, -1);
    for (long long int i = 1; i <= n; i++) {
      cin >> a[i];
      mrg(i, a[i]);
    }
    for (long long int i = 1; i <= n; i++) {
      long long int k = root(i);
      cout << -par[k] << " ";
    }
    cout << endl;
  }
  return 0;
}
