#include <bits/stdc++.h>
using namespace std;
void min_self(int& a, int b) { a = min(a, b); }
void max_self(int& a, int b) { a = max(a, b); }
const int NAX = (int)(2e5 + 5);
int v[NAX], visited[NAX];
int component_size;
void visit(int i) {
  if (!visited[i]) {
    ++component_size;
    visited[i] = 1;
    visit(v[i]);
    visited[i] = component_size;
    return;
  }
  return;
}
int main() {
  int t, n;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d", &v[i]);
    for (int i = 1; i <= n; ++i) {
      component_size = 0;
      visit(i);
    }
    for (int i = 1; i <= n; ++i) printf("%d ", visited[i]);
    printf("\n");
    for (int i = 1; i <= n; ++i) visited[i] = 0;
  }
  return 0;
}
