#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    int N;
    cin >> N;
    vector<int> arr(N);
    for (int i = 0; i < N; i++) {
      cin >> arr[i];
      arr[i]--;
    }
    vector<bool> visited(N, false);
    vector<int> ans(N);
    for (int i = 0; i < N; i++) {
      if (visited[i]) {
        continue;
      }
      vector<int> cycles;
      int v = i;
      while (!visited[i]) {
        cycles.push_back(i);
        visited[i] = true;
        i = arr[i];
      }
      for (int j = 0; j < cycles.size(); j++) {
        ans[cycles[j]] = cycles.size();
      }
    }
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i] << " ";
    }
    cout << endl;
  }
}
