#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> visited(n);
    vector<int> a(n);
    vector<int> ans(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      a[i]--;
    }
    for (int i = 0; i < n; i++) {
      if (visited[i] == 0) {
        vector<int> v;
        while (!visited[i]) {
          v.push_back(i);
          visited[i] = 1;
          i = a[i];
        }
        for (auto x : v) ans[x] = v.size();
      }
    }
    for (int i = 0; i < n; i++) cout << ans[i] << " ";
    cout << endl;
  }
}
