#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
void all_set() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); }
int main() {
  all_set();
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> v(n + 1), res(n + 1);
    for (int i = 1; i <= n; i++) cin >> v[i];
    for (int i = 1; i <= n; i++) {
      if (res[i] > 0) continue;
      int c = 1;
      int chk = v[i];
      while (chk != i) {
        c++;
        chk = v[chk];
      }
      do {
        res[chk] = c;
        chk = v[chk];
      } while (chk != i);
    }
    for (int i = 1; i <= n; i++) cout << res[i] << " ";
    cout << '\n';
  }
  return 0;
}
