#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, n, xx, index, count;
  vector<int> x, ans;
  cin >> q;
  while (q--) {
    cin >> n;
    for (int i = 0; i < n; i++) ans.push_back(0);
    for (int i = 0; i < n; i++) {
      cin >> xx;
      x.push_back(xx - 1);
    }
    for (int i = 0; i < n; i++) {
      if (ans[i] != 0) continue;
      index = x[i], count = 1;
      while (index != i) index = x[index], count++;
      while (ans[index] != count) ans[index] = count, index = x[index];
    }
    for (int i = 0; i < n; i++) cout << ans[i] << ' ';
    endl(cout);
    x.clear(), ans.clear();
  }
}
