#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
struct UnionFind {
  vector<int> par, siz;
  UnionFind(int N) : par(N), siz(N) {
    for (int i = (int)(0); i < (int)(N); ++i) {
      par[i] = i;
      siz[i] = 1;
    }
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    else
      return root(par[x]);
  }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return;
    if (siz[x] < siz[y]) swap(x, y);
    par[y] = x;
    siz[x] += siz[y];
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return siz[root(x)]; }
};
void solve() {
  int N;
  cin >> N;
  UnionFind uf(N);
  for (int i = (int)(0); i < (int)(N); ++i) {
    int a;
    cin >> a;
    a--;
    uf.unite(a, i);
  }
  for (int i = (int)(0); i < (int)(N); ++i) {
    if (i != 0) cout << " ";
    cout << uf.size(i);
  }
  cout << endl;
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    solve();
  }
}
