#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " , ";
  __f(comma + 1, args...);
}
inline long long int gcd(long long int a, long long int b) {
  a = abs(a), b = abs(b);
  while (b) a = a % b, swap(a, b);
  return a;
}
inline long long int power(long long int a, long long int p) {
  long long int res = 1, x = a;
  while (p) {
    if (p & 1) res = (res * x);
    x = (x * x), p >>= 1;
  }
  return res;
}
inline long long int big_mod(long long int a, long long int p,
                             long long int m) {
  long long int res = 1 % m, x = a % m;
  while (p) {
    if (p & 1) res = (res * x) % m;
    x = (x * x) % m, p >>= 1;
  }
  return res;
}
const long long int INF = 1e15;
const int nax = 2e5 + 5;
int parent[nax];
int sz[nax];
void makeset(int u) {
  parent[u] = u;
  sz[u] = 1;
}
void init_DSU(int N) {
  for (int i = 0; i <= N; i++) {
    makeset(i);
  }
}
int Find(int r) {
  if (parent[r] == r) {
    return r;
  }
  parent[r] = Find(parent[r]);
  return parent[r];
}
int FindSize(int r) { return sz[Find(r)]; }
void Union(int a, int b) {
  int u = Find(a);
  int v = Find(b);
  if (u != v) {
    parent[u] = v;
    sz[v] += sz[u];
  }
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    init_DSU(n);
    for (int i = 1; i <= n; i++) {
      int x;
      cin >> x;
      Union(i, x);
    }
    for (int i = 1; i <= n; i++) {
      cout << FindSize(i) << " ";
    }
    cout << endl;
  }
  return 0;
}
