#include <bits/stdc++.h>
using namespace std;
int vis[1000001];
vector<int> ar[1000001];
int dis[1000001];
int c;
void dfs(int s) {
  vis[s] = 1;
  c++;
  for (int child : ar[s]) {
    if (vis[child] == 0) {
      dfs(child);
    }
  }
  dis[s] = c;
}
int main() {
  int t, m, p[1000001];
  cin >> t;
  while (t--) {
    cin >> m;
    for (int i = 1; i <= m; i++) {
      vis[i] = 0;
      ar[i].clear();
      p[i] = 0;
    }
    for (int i = 1; i <= m; i++) {
      cin >> p[i];
      if (i != p[i]) ar[i].push_back(p[i]);
    }
    for (int i = 1; i <= m; i++) {
      if (vis[i] == 0) {
        c = 0;
        dfs(i);
      }
    }
    for (int i = 1; i <= m; i++) {
      cout << dis[i] << " ";
    }
    cout << endl;
  }
  return 0;
}
