#include <bits/stdc++.h>
using namespace std;
int x, rep[200001], p[200001], r[200001], q;
int main() {
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> p[i];
    }
    for (int i = 1; i <= n; i++) {
      if (!rep[i]) {
        rep[i] = i;
        int k = i;
        k = p[i];
        while (k != i) {
          rep[k] = i;
          r[i]++;
          k = p[k];
        }
      }
    }
    for (int i = 1; i <= n; i++) cout << r[rep[i]] + 1 << " ";
    cout << "\n";
    for (int i = 1; i <= n; i++) {
      rep[i] = 0;
      r[i] = 0;
    }
  }
  return 0;
}
