#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long binExp(long long x, long long n) {
  long long r = 1;
  while (n > 0) {
    if (n % 2 == 1) r = r * x;
    x = x * x;
    n = n / 2;
  }
  return r;
}
long long modbinExp(long long x, long long n, long long M) {
  long long r = 1;
  while (n > 0) {
    if (n % 2 == 1) r = (r * x) % M;
    x = (x * x) % M;
    n = n / 2;
  }
  return r;
}
vector<long long> vec(200005), v(200005);
long long vis[200005], ct[200005];
void dfs(long long s, long long cnt) {
  vis[s] = 1;
  vec[s] = cnt;
  if (vis[v[s]] == 0) dfs(v[s], cnt);
}
void solve() {
  long long n;
  cin >> n;
  vector<long long> ans(n + 1);
  for (long long i = 1; i <= n; i++) {
    cin >> v[i];
    vis[i] = 0;
    ct[i] = 0;
  }
  long long tt = 1;
  for (long long i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      dfs(v[i], tt);
      tt++;
    }
  }
  for (long long i = 1; i <= n; i++) ct[vec[i]]++;
  for (long long i = 1; i <= n; i++) cout << ct[vec[i]] << " ";
  cout << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}
