#include <bits/stdc++.h>
using namespace std;
int main() {
  ifstream in("a.in");
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    int arr[n + 1], vis[n + 1], ans[n + 1];
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; i++) cin >> arr[i];
    for (int i = 1; i <= n; i++) {
      if (!vis[i]) {
        int cur = arr[i], cnt = 1;
        vector<int> v;
        v.push_back(i);
        while (cur != i) {
          v.push_back(cur);
          cur = arr[cur];
          cnt++;
        }
        for (int j = 0; j < v.size(); j++) ans[v[j]] = cnt;
      }
    }
    for (int i = 1; i <= n; i++) cout << ans[i] << " ";
    cout << endl;
  }
}
