#include <bits/stdc++.h>
using namespace std;
long long par[400000];
long long r[400000] = {0};
long long find_fn(long long i) {
  if (par[i] == -1) return i;
  return par[i] = find_fn(par[i]);
}
void union_fn(long long x, long long y) {
  long long s1 = find_fn(x);
  long long s2 = find_fn(y);
  if (s1 != s2) {
    if (r[s1] > r[s2]) {
      par[s2] = s1;
      r[s1] += r[s2];
    } else {
      par[s1] = s2;
      r[s2] += r[s1];
    }
  }
}
void solve() {
  long long n, i;
  cin >> n;
  long long a[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    r[i] = 1;
    par[i] = -1;
  }
  for (long long i = 0; i < n; i++) {
    union_fn(i, a[i]);
  }
  for (long long i = 0; i < n; i++) {
    cout << r[find_fn(i)] << " ";
  }
}
int32_t main() {
  long long n, m, i, flag = 0, j, sum = 0, x, y, r, ans = 0, a, b, c, z, s = 1,
                     e, mid, mx = INT_MIN;
  long long t;
  cin >> t;
  while (t--) {
    solve();
    cout << endl;
  }
  return 0;
}
