#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll INF = 1LL << 60;
constexpr int MOD = 998244353;
struct UnionFind {
  vector<int> par, weight;
  UnionFind(int n) : par(n, -1), weight(n) {}
  void unite(int x, int y, int w = 0) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (par[x] > par[y]) swap(x, y), w = -w;
    par[x] += par[y];
    par[y] = x;
    return;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int find(int x) {
    if (par[x] < 0) return x;
    int r = find(par[x]);
    return par[x] = r;
  }
  int sizeOf(int x) { return -par[find(x)]; }
};
int q, n, a[200020], b[200020];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> q;
  while (q--) {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    UnionFind uf(n + 1);
    for (int i = 1; i <= n; ++i) uf.unite(i, a[i]);
    for (int i = 1; i <= n; ++i) cout << uf.sizeOf(i) << " \n"[n == i];
  }
  return 0;
}
