#include <bits/stdc++.h>
using namespace std;
int p[200001], r[200001], sz[200001];
void init() {
  for (int i = 0; i < 200001; i++) p[i] = i, r[i] = 0, sz[i] = 1;
}
int root(int i) { return p[i] == i ? i : (p[i] = root(p[i])); }
bool join_r(int i, int j) {
  int x = root(i);
  int y = root(j);
  if (x == y) return false;
  if (r[x] > r[y]) {
    p[y] = x;
    sz[x] += sz[y];
  } else {
    p[x] = y;
    sz[y] += sz[x];
    if (r[x] == r[y]) {
      r[y]++;
    }
  }
  return true;
}
int tc, n, a[200001];
int main() {
  cin >> tc;
  while (tc--) {
    init();
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      join_r(a[i], i);
    }
    for (int i = 1; i <= n; i++) {
      int root_i = root(i);
      cout << sz[root_i] << " ";
    }
    cout << "\n";
  }
  return 0;
}
