#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn], ff[maxn];
int fun(int st, int now, int to, int step) {
  if (st == to) return ff[now] = step;
  return ff[now] = fun(st, to, a[to], step + 1);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    memset(ff, 0, sizeof(int) * (n + 1));
    for (int i = 1; i <= n; ++i)
      if (!ff[i]) fun(i, i, a[i], 1);
    for (int i = 1; i <= n; ++i) cout << ff[i] << " ";
    cout << '\n';
  }
  return 0;
}
