#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b) {
  if (b == 0) return 1;
  long long int temp = power(a, b >> 1);
  if (b & 1)
    return (a * temp * temp);
  else
    return temp * temp;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int q;
  cin >> q;
  while (q--) {
    long long int n;
    cin >> n;
    long long int arr[n];
    for (long long int i = 0; i < n; i++) cin >> arr[i];
    long long int ans[n];
    for (long long int i = 0; i < n; i++) ans[i] = -1;
    for (long long int i = 0; i < n; i++) {
      if (ans[i] == -1) {
        long long int temp = i + 1;
        long long int count = 1;
        long long int j = i;
        while (arr[j] != temp) {
          j = arr[j] - 1;
          count++;
        }
        j = i;
        while (arr[j] != temp) {
          j = arr[j] - 1;
          ans[j] = count;
        }
        ans[i] = count;
      }
    }
    for (long long int i = 0; i < n; i++) cout << ans[i] << " ";
    cout << "\n";
  }
  return 0;
}
