#include <bits/stdc++.h>
using namespace std;
int f[200050], g[200050];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  for (; t != 0; t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> f[i];
    for (int i = 1; i <= n; i++) {
      if (g[i] == t) {
        cout << f[i] << " ";
        continue;
      }
      queue<int> Q;
      int p = i;
      do {
        Q.push(p);
        p = f[p];
      } while (p != i);
      f[i] = Q.size();
      while (!Q.empty()) f[Q.front()] = f[i], g[Q.front()] = t, Q.pop();
      cout << f[i] << " ";
    }
    cout << "\n";
  }
  return 0;
}
