#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1e9;
int ans[300005];
int solve(int v[], int i, int c) {
  c++;
  if (ans[v[i]] == 1) return ans[i] = c;
  return ans[i] = solve(v, v[i], c);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tt = 1;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    int v[n + 1];
    for (int i = 1; i <= n; i++) {
      cin >> v[i];
      ans[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      if (ans[i] == 0) {
        ans[i] = 1;
        solve(v, i, 0);
      }
    }
    for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
    cout << endl;
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  ;
  return 0;
}
