#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long q;
  cin >> q;
  while (q--) {
    long long n;
    cin >> n;
    long long a[n + 1];
    for (long long i = 1; i <= n; i++) cin >> a[i];
    vector<long long> vis(n + 1, 0);
    vector<long long> ans(n + 1, 0);
    for (long long i = 1; i <= n; i++) {
      long long cy = 0;
      long long j = i;
      while (!vis[j]) {
        cy++;
        vis[j] = 1;
        j = a[j];
      }
      for (long long k = 1; k <= n; k++) {
        if (vis[k] && ans[k] == 0) ans[k] = cy;
      }
    }
    for (long long i = 1; i <= n; i++) cout << ans[i] << " ";
    cout << endl;
  }
  return 0;
}
