#include <bits/stdc++.h>
using namespace std;
inline long long add(long long a, long long b) {
  return ((a % 1000000007) + (b % 1000000007)) % 1000000007;
}
inline long long mul(long long a, long long b) {
  return ((a % 1000000007) * (b % 1000000007)) % 1000000007;
}
inline long long sub(long long a, long long b) {
  return ((a % 1000000007) - (b % 1000000007) + 1000000007) % 1000000007;
}
struct union_find {
  vector<long long> parent;
  vector<long long> size;
  union_find(long long n) {
    parent = vector<long long>(n);
    size = vector<long long>(n);
    for (long long i = 0; i < n; i++) {
      parent[i] = i;
      size[i] = 1;
    }
  }
  long long find(long long x) {
    if (parent[x] == x) {
      return x;
    } else {
      parent[x] = find(parent[x]);
      return parent[x];
    }
  }
  void unite(long long x, long long y) {
    long long rx = find(x);
    long long ry = find(y);
    if (rx == ry) return;
    if (size[rx] < size[ry]) {
      parent[rx] = ry;
      size[ry] += size[rx];
    } else {
      parent[ry] = rx;
      size[rx] += size[ry];
    }
  }
};
void solve() {
  long long n;
  cin >> n;
  vector<long long> p(n);
  for (long long i = 0; i < n; i++) cin >> p[i];
  union_find u = union_find(n);
  for (long long i = 0; i < n; i++) u.unite(i, p[i] - 1);
  map<long long, long long> m;
  for (long long i = 0; i < n; i++) m[u.find(i)]++;
  for (long long i = 0; i < n; i++) cout << m[u.find(i)] << " ";
  cout << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  cin >> t;
  for (long long a = 0; a < t; a++) {
    solve();
  }
}
