#include <bits/stdc++.h>
using namespace std;
void CyBerForCe() {
  long long n;
  cin >> n;
  vector<long long> v, ans(n);
  for (long long i = 0; i < n; i++) {
    long long a;
    cin >> a;
    v.push_back(a);
  }
  vector<long long> used(n, false);
  for (long long i = 0; i < n; i++) {
    if (!used[i]) {
      vector<long long> s;
      while (!used[i]) {
        s.push_back(i);
        used[i] = true;
        i = v[i] - 1;
      }
      for (auto j : s) ans[j] = s.size();
    }
  }
  for (auto i : ans) cout << i << " ";
  cout << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) CyBerForCe();
  return 0;
}
