#include <bits/stdc++.h>
using namespace std;
const long double Pi = 3.141592653;
const long long mod = 1e6 + 3;
class Graph {
  map<int, vector<int>> ma;

 public:
  void addedge(int n, int m) {
    ma[n].push_back(m);
    ma[m].push_back(n);
  }
};
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      --a[i];
    }
    vector<int> used(n);
    vector<long long> ans(n);
    for (long long i = 0; i < n; ++i) {
      if (!used[i]) {
        vector<long long> cur;
        while (!used[i]) {
          cur.push_back(i);
          used[i] = true;
          i = a[i];
        }
        for (auto e : cur) ans[e] = cur.size();
      }
    }
    for (long long i = 0; i < n; i++) {
      cout << ans[i] << " ";
    }
    cout << endl;
  }
  return 0;
}
