#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int q, n, i, current = 0, k;
  cin >> q;
  while (q > 0) {
    q--;
    cin >> n;
    int arr[n];
    int ans[n];
    vector<int> v;
    bitset<200000> b;
    for (i = 0; i < n; i++) {
      cin >> arr[i];
    }
    for (i = 0; i < n; i++) {
      if (b[i] == 1) {
        continue;
      }
      current = i;
      do {
        b[current] = 1;
        v.push_back(current);
        current = arr[current] - 1;
      } while (current != i);
      for (k = 0; k < v.size(); k++) {
        ans[v[k]] = v.size();
      }
      v.clear();
    }
    for (i = 0; i < n; i++) {
      cout << ans[i] << " ";
    }
    cout << endl;
  }
}
