#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 70;
int a[maxn];
int n;
int vis[maxn];
int f, cnt;
void dfs(int x) {
  cnt++;
  if (a[x] == f) return;
  dfs(a[x]);
}
void dfs2(int x) {
  vis[x] = cnt;
  if (a[x] == f) return;
  dfs2(a[x]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tst;
  cin >> tst;
  while (tst--) {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    memset(vis, -1, sizeof vis);
    for (int i = 1; i <= n; i++) {
      if (vis[i] == -1) {
        f = i, cnt = 0, dfs(i);
        dfs2(i);
      }
      cout << vis[i] << " ";
    }
    cout << endl;
  }
  return 0;
}
