#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-12;
const double pi = 3.141592653589793;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int t, n;
int parent[N];
int connections[N];
int vis[N];
int find_set(int s) {
  if (parent[s] == s) return s;
  return parent[s] = find_set(parent[s]);
}
void link(int u, int v) {
  if (connections[u] > connections[v]) swap(u, v);
  parent[u] = v;
  if (connections[u] <= connections[v]) {
    connections[v] += connections[u];
    connections[u] = connections[v];
  }
}
void union_set(int u, int v) {
  u = find_set(u);
  v = find_set(v);
  if (u != v) link(u, v);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    memset(parent, 0, sizeof(parent));
    memset(connections, 1, sizeof(connections));
    memset(vis, 0, sizeof(vis));
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      int u;
      cin >> u;
      if (!vis[i]) {
        parent[i] = i;
        connections[i] = 1;
        vis[i] = 1;
      }
      if (!vis[u]) {
        parent[u] = u;
        connections[u] = 1;
        vis[u] = 1;
      }
      union_set(i, u);
    }
    for (int i = 1; i <= n; ++i) cout << connections[find_set(i)] << " ";
    cout << endl;
  }
}
