#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int a[200001];
bool visited[200001];
void dfs(int n) {
  if (visited[n] == true) {
    return;
  }
  visited[n] = true;
  v.push_back(n);
  dfs(a[n - 1]);
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    long long n, i, j;
    cin >> n;
    long long ans[n];
    for (i = 0; i < n; i++) {
      cin >> a[i];
    }
    memset(visited, false, sizeof(visited));
    for (i = 0; i < n; i++) {
      v.clear();
      if (visited[a[i]] == false) {
        dfs(a[i]);
        for (j = 0; j < v.size(); j++) {
          ans[v[j] - 1] = v.size();
        }
      }
    }
    for (i = 0; i < n; i++) {
      cout << ans[i] << " ";
    }
    cout << endl;
  }
}
