#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
const int maxn = 2e5 + 10;
int a[maxn];
int ans[maxn], vis[maxn], father[maxn];
int Find(int idx) {
  if (father[idx] == idx) return idx;
  father[idx] = Find(father[idx]);
  return father[idx];
}
void combine(int a, int b) {
  int fa = Find(a);
  int fb = Find(b);
  if (fa == fb)
    return;
  else {
    father[fa] = fb;
    ans[fb] += ans[fa];
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    memset(vis, 0, sizeof(vis));
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
      father[i] = i;
      ans[i] = 1;
    }
    for (int i = 1; i <= n; ++i) {
      combine(i, a[i]);
    }
    for (int i = 1; i <= n; ++i) {
      cout << ans[Find(i)] << " ";
    }
    cout << endl;
  }
  return 0;
}
