#include <bits/stdc++.h>
using namespace std;
int n, ary[200000 + 5], dp[200000 + 5];
int go(int x, int s, int c) {
  if (x == s) return dp[x] = c;
  return dp[x] = go(ary[x], s, c + 1);
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  while (q--) {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> ary[i];
    memset(dp, 0, sizeof(dp));
    for (int i = 1; i <= n; ++i)
      if (dp[i] == 0) {
        go(ary[i], i, 1);
      }
    for (int i = 1; i <= n; ++i) cout << dp[i] << " ";
    cout << "\n";
  }
  return 0;
}
