#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
int a[MAXN], pre[MAXN], sz[MAXN];
int find(int x) { return pre[x] == x ? x : pre[x] = find(pre[x]); }
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
      pre[i] = i, sz[i] = 1;
    }
    for (int i = 1; i <= n; ++i) {
      if (find(i) != find(a[i])) {
        int x = find(i);
        int y = find(a[i]);
        pre[x] = y;
        sz[y] += sz[x];
      }
    }
    for (int i = 1; i <= n; ++i) {
      cout << sz[find(i)] << ' ';
    }
    cout << '\n';
  }
  return 0;
}
