#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> v(n + 1);
    for (int i = 1; i <= n; i++) cin >> v[i];
    vector<int> ans(n + 1, 0), vis(n + 1, 0);
    for (int i = 1; i <= n; i++) {
      vector<int> temp;
      int x = i;
      while (v[x] != i && vis[i] == 0) {
        x = v[x];
        vis[v[x]] = 1;
        temp.push_back(v[x]);
      }
      for (int j = 0; j < temp.size(); j++) ans[temp[j]] = temp.size();
    }
    for (int i = 1; i <= n; i++) cout << ans[i] + 1 << " ";
    cout << endl;
  }
}
