#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
const long long N = 2e5 + 1;
long long par[N] = {0};
long long sz[N] = {0};
void make_set(long long a) {
  par[a] = a;
  sz[a] = 1;
}
long long find_set(long long a) {
  if (a == par[a])
    return a;
  else
    return par[a] = find_set(par[a]);
}
void merge_set(long long a, long long b) {
  a = find_set(a);
  b = find_set(b);
  if (a == b) {
    return;
  }
  if (sz[a] < sz[b]) {
    swap(a, b);
  }
  par[b] = a;
  sz[a] += sz[b];
}
void mysol() {
  long long n;
  cin >> n;
  long long arr[N];
  for (long long i = 1; i <= n; i++) {
    long long a;
    cin >> a;
    arr[i] = a;
    make_set(a);
  }
  for (long long i = 1; i <= n; i++) {
    merge_set(arr[i], i);
  }
  for (long long i = 1; i <= n; i++) {
    cout << sz[find_set(i)] << ' ';
  }
  cout << '\n';
  memset(sz, 0, sizeof(sz));
  memset(par, 0, sizeof(par));
}
int32_t main() {
  fast();
  long long t;
  cin >> t;
  while (t-- > 0) {
    mysol();
  }
  return 0;
}
