#include <bits/stdc++.h>
using namespace std;
vector<long long int> v, vans;
long long int visited[200005] = {0}, cnt = 0, ans[200005] = {0};
long long int dfs(long long int x) {
  visited[x] = 1;
  vans.push_back(x);
  long long int i, j, k, l;
  cnt++;
  if (visited[v[x]] == 0) {
    ans[v[x]] = dfs(v[x]);
  } else {
    return cnt;
  }
}
int main() {
  long long int q;
  cin >> q;
  while (q--) {
    v.clear();
    vans.clear();
    cnt = 0;
    memset(visited, 0, sizeof(visited));
    long long int i, j, k, l, n;
    cin >> n;
    long long int a;
    v.push_back(0);
    for (i = 1; i <= n; i++) {
      cin >> a;
      v.push_back(a);
    }
    for (i = 1; i <= n; i++) {
      cnt = 0;
      if (v[i] == i) {
        ans[i] = 1;
      } else if (visited[i] == 0) {
        vans.clear();
        ans[i] = dfs(i);
      }
    }
    for (i = 1; i <= n; i++) {
      cout << ans[i] << " ";
    }
    cout << endl;
  }
}
