#include <bits/stdc++.h>
using namespace std;
const int MXN = 200005;
int par[MXN];
int sz[MXN];
int n;
void setup(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    sz[i] = 1;
  }
}
int parent(int n) {
  if (par[n] == n) return n;
  return par[n] = parent(par[n]);
}
bool arc(int a, int b) { return parent(a) == parent(b); }
int gsi(int a) { return sz[parent(a)]; }
void connect(int a, int b) {
  if (arc(a, b)) return;
  int ax = parent(a);
  int bx = parent(b);
  par[ax] = bx;
  sz[bx] += sz[ax];
}
void solve() {
  int n;
  cin >> n;
  setup(n + 1);
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    connect(i, x);
  }
  for (int i = 1; i <= n; i++) {
    cout << gsi(i) << ' ';
  }
  cout << endl;
}
int main() {
  int q;
  cin >> q;
  while (q--) solve();
}
