#include <bits/stdc++.h>
using namespace std;
void dfs(int u, int par, int &cnt, vector<int> adj[], vector<int> &vis,
         vector<int> &ans) {
  vis[u] = 1;
  for (auto v : adj[u]) {
    if (v == par) continue;
    if (not vis[v]) {
      ans.push_back(v);
      cnt++;
      dfs(v, u, cnt, adj, vis, ans);
    }
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> adj[n + 1];
    vector<int> dis;
    dis.resize(n + 1, 0);
    vector<int> vis;
    vis.resize(n + 1, 0);
    for (int i = 1; i <= n; ++i) {
      int a;
      cin >> a;
      if (a != i) {
        int flag = 0;
        for (auto it : adj[i]) {
          if (it == a) {
            flag = 1;
            break;
          }
        }
        if (flag == 0) {
          adj[i].push_back(a);
          adj[a].push_back(i);
        }
      }
    }
    for (int i = 1; i <= n; ++i) {
      if (vis[i] == 0) {
        int cnt = 1;
        vector<int> ans;
        dfs(i, 0, cnt, adj, vis, ans);
        dis[i] = cnt;
        for (auto it : ans) {
          dis[it] = cnt;
          vis[it] = 1;
        }
      }
    }
    for (int i = 1; i <= n; ++i) cout << dis[i] << " ";
    cout << endl;
  }
  return 0;
}
