#include <bits/stdc++.h>
using namespace std;
vector<int> par;
void init_dsu(int n) {
  par.clear();
  par.resize(n, -1);
}
int root(int x) { return par[x] < 0 ? x : par[x] = root(par[x]); }
void Union(int x, int y) {
  if ((x = root(x)) == (y = root(y))) return;
  if (par[y] < par[x]) swap(x, y);
  par[x] += par[y];
  par[y] = x;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    init_dsu(n);
    for (int i = 0; i < n; i++) {
      int u;
      cin >> u;
      Union(i, u - 1);
    }
    for (int i = 0; i < n; i++) {
      int x = root(i);
      cout << abs(par[x]) << " ";
    }
    cout << "\n";
  }
  return 0;
}
