#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N], vis[N], b[N];
int ans;
void dfs(int i) {
  if (vis[i]) return;
  vis[i] = 1;
  ans++;
  dfs(a[i]);
  b[i] = ans;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
    }
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; i++) {
      ans = 0;
      dfs(i);
      if (i != n)
        printf("%d ", b[i]);
      else
        printf("%d\n", b[i]);
    }
  }
  return 0;
}
