#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
inline long long gcd(long long a, long long b) {
  return b == 0 ? a : gcd(b, a % b);
}
inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
inline long long ceil(long long a, long long b) {
  return (a % b == 0) ? a / b : a / b + 1;
}
long long powerm(long long x, long long y) {
  if (y == 0) return 1;
  x = x % mod;
  if (y % 2 == 0) return powerm(x * x, y / 2) % mod;
  return (x * powerm(x * x, y / 2)) % mod;
}
clock_t time_p = clock();
void time() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "sec\n";
}
vector<long long> parent;
vector<long long> ranks;
long long n;
long long findparent(long long node) {
  if (parent[node] == node) {
    return node;
  }
  return parent[node] = findparent(parent[node]);
}
void dounion(long long node1, long long node2) {
  long long a = findparent(node1);
  long long b = findparent(node2);
  if (a == b) return;
  if (ranks[a] > ranks[b]) {
    parent[b] = a;
    ranks[a] += ranks[b];
  } else if (ranks[a] < ranks[b]) {
    parent[a] = b;
    ranks[b] += ranks[a];
  } else {
    parent[a] = b;
    ranks[b] += ranks[a];
  }
}
void solve() {
  cin >> n;
  parent.resize(n + 1);
  ranks.resize(n + 1);
  long long arr[n + 1];
  map<long long, long long> m;
  vector<pair<long long, long long>> v;
  for (long long i = 1; i <= n; i++) {
    parent[i] = i;
    ranks[i] = 1;
  }
  for (long long i = 1; i <= n; i++) {
    cin >> arr[i];
    dounion(i, arr[i]);
  }
  for (long long i = 1; i <= n; i++) {
    cout << ranks[findparent(arr[i])] << " ";
  }
  cout << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  time();
}
