#include <bits/stdc++.h>
using namespace std;
int arr[200000 + 100];
vector<int> v(200000 + 100);
int number(int i, int j) {
  if (v[i] == j) return 1;
  return 1 + number(v[i], j);
}
void initialize(int i, int j, int n) {
  if (v[i] == j)
    arr[i] = n;
  else {
    arr[i] = n;
    initialize(v[i], j, n);
  }
}
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> v[i];
    for (int i = 1; i <= n; i++) {
      if (arr[i] == 0) initialize(i, i, number(i, i));
    }
    for (int i = 1; i <= n; i++) {
      cout << arr[i] << " ";
      arr[i] = 0;
    }
    cout << endl;
  }
}
