#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int mxn = 2e5;
vector<int> adj[mxn];
vector<bool> vis(mxn, 1);
vector<int> val(mxn, 1);
void dfs(int v, int d, vector<int> &x) {
  vis[v] = 0;
  x.push_back(v);
  bool aux = 1;
  for (int u : adj[v]) {
    aux &= !vis[u];
    if (vis[u]) dfs(u, d + 1, x);
  }
  if (aux) {
    for (int i = 0; i < int(x.size()); ++i) {
      val[x[i]] = d;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    memset(adj, 0, sizeof(adj));
    for (int i = 0, x; i < n; ++i) {
      cin >> x;
      --x;
      vis[i] = 1;
      val[i] = 1;
      adj[i].push_back(x);
    }
    for (int i = 0; i < n; ++i) {
      if (vis[i]) {
        vector<int> x;
        dfs(i, 1, x);
      }
    }
    for (int i = 0; i < n; ++i) {
      if (i) cout << ' ';
      cout << val[i];
    }
    cout << '\n';
  }
  return 0;
}
