#include <bits/stdc++.h>
using namespace std;
int N, M, sz, h;
int a[200001];
int b[200001];
int c[200001];
int main() {
  cin >> N;
  for (int j = 0; j < N; j++) {
    cin >> M;
    for (int i = 1; i <= M; i++) {
      cin >> c[i];
    }
    sz = 1;
    for (int i = 1; i <= M; i++) {
      if ((a[i] == 0) && (a[c[i]] == 0)) {
        a[i] = sz;
        b[a[i]]++;
        h = i;
        while (a[c[h]] != sz) {
          a[c[h]] = sz;
          h = c[c[h]];
          b[a[i]]++;
        }
        sz++;
      } else {
        if (a[c[i]] == 0) {
          a[c[i]] = a[i];
          b[a[i]]++;
        }
        if (a[i] == 0) {
          a[i] = a[c[i]];
          b[a[c[i]]]++;
        }
      }
    }
    for (int i = 1; i <= M; i++) {
      cout << b[a[i]] << " ";
    }
    cout << '\n';
    for (int i = 1; i <= M; i++) {
      a[i] = 0;
      b[i] = 0;
    }
  }
  return 0;
}
