#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn], ans[maxn];
void dfs(int u, int cnt, int goal) {
  if (a[u] == goal) {
    ans[u] = cnt;
    return;
  }
  dfs(a[u], cnt + 1, goal);
  ans[u] = ans[a[u]];
}
int main() {
  memset(ans, -1, sizeof ans);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) {
      if (ans[i] == -1) {
        dfs(i, 1, i);
      }
    }
    for (int i = 1; i <= n; i++) {
      cout << ans[i];
      ans[i] = -1;
      if (i == n)
        cout << "\n";
      else
        cout << " ";
    }
  }
  return 0;
}
