#include <bits/stdc++.h>
using namespace std;
long long visited[210000] = {0};
vector<long long> vg[210000], v1;
long long i, l = 0;
void dfs(long long current) {
  long long j, y;
  visited[current] = 1;
  for (j = 0; j < vg[current].size(); j++) {
    y = vg[current][j];
    if (visited[y] == 0) l++, v1.push_back(y), dfs(y);
  }
}
void lakshay() {
  long long z = 0, c, d, y = 0, x, A[1000], B[1000], a, b, s = 0, j, m, n, p, q,
            k, r;
  bool ok = true;
  map<long long, long long> M;
  cin >> n;
  for (i = 1; i <= n; i++) vg[i].clear(), visited[i] = 0;
  for (i = 1; i <= n; i++) {
    cin >> k;
    vg[i].push_back(k);
  }
  for (i = 1; i <= n; i++) {
    if (!visited[i]) {
      l = 0;
      v1.push_back(i);
      dfs(i);
      for (auto x : v1) M[x] = l + 1;
      v1.clear();
    }
  }
  for (i = 1; i <= n; i++) cout << M[i] << " ";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long num;
  cin >> num;
  while (num--) {
    lakshay();
    cout << "\n";
  }
  return 0;
}
