#include <bits/stdc++.h>
using namespace std;
int cnt = 0;
vector<bool> visited;
vector<vector<int>> adjList;
vector<int> test;
void dfs(int a) {
  visited[a] = true;
  cnt++;
  test.push_back(a);
  for (int i = 0; i < adjList[a].size(); i++) {
    int temp = adjList[a][i];
    if (!visited[temp]) dfs(temp);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int number;
  cin >> number;
  while (number--) {
    int many;
    cin >> many;
    adjList.clear();
    visited.assign(many, false);
    adjList.resize(many);
    int arr[many];
    for (int i = 0; i < many; i++) {
      int temp;
      cin >> temp;
      temp--;
      adjList[i].push_back(temp);
    }
    for (int i = 0; i < many; i++) {
      if (!visited[i]) {
        cnt = 0;
        test.clear();
        dfs(i);
        for (int j = 0; j < test.size(); j++) arr[test[j]] = cnt;
      }
    }
    for (int i = 0; i < many; i++) {
      cout << arr[i];
      if (i != many - 1) cout << " ";
    }
    cout << endl;
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n";
}
