#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    pqpq2;
vector<pair<long long, pair<long long, long long> > > vpp;
vector<pair<long long, long long> > vp;
const long long INF = 10e12;
const int N = 2e5 + 5;
int root[N], sz[N];
int n;
void init() {
  for (int i = 1; i <= n; i++) {
    root[i] = i;
    sz[i] = 1;
  }
}
int rt(int k) {
  while (k != root[k]) {
    root[k] = root[root[k]];
    k = root[k];
  }
  return k;
}
void marge(int u, int v) {
  int rt1 = rt(u);
  int rt2 = rt(v);
  if (rt1 == rt2) return;
  if (sz[rt1] > sz[rt2]) swap(rt1, rt2);
  sz[rt2] += sz[rt1];
  sz[rt1] = 0;
  root[rt1] = root[rt2];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  map<int, int> mp;
  while (t--) {
    cin >> n;
    init();
    int ara[n + 2];
    for (int i = 1; i <= n; i++) {
      int nu;
      cin >> ara[i];
    }
    int ans[n + 2];
    for (int i = 1; i <= n; i++) {
      int u = i;
      int v = ara[i];
      while (rt(u) != rt(v)) {
        marge(u, v);
        v = ara[u];
        u = v;
      }
    }
    for (int i = 1; i <= n; i++) {
      cout << sz[rt(ara[i])] << " ";
    }
    cout << "\n";
  }
  return 0;
}
