#include <bits/stdc++.h>
using namespace std;
int a[200005];
int b[200005];
bool f[200005];
int main() {
  int q, n;
  cin >> q;
  while (q--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      b[i] = 1;
      f[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      int j = i;
      if (f[i] == 1) continue;
      f[i] = 1;
      int ctr = 1;
      while (a[j] != i) {
        ctr++;
        f[j] = 1;
        j = a[j];
      }
      j = i;
      while (a[j] != i) {
        b[j] = ctr;
        j = a[j];
      }
    }
    for (int i = 1; i <= n; i++) cout << b[i] << " ";
    cout << "\n";
  }
}
