#include <bits/stdc++.h>
const int maxn = 1e6 + 10;
const int maxm = 1e5 + 10;
const int mod = 1e9 + 7;
const long long int INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int a[maxn], ans[maxn], pre[maxn];
int find(int x) { return x == pre[x] ? x : pre[x] = find(pre[x]); }
void Union(int x, int y) { pre[find(x)] = pre[find(y)]; }
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    for (int i = 0; i <= n; i++) pre[i] = i, ans[i] = 0;
    for (int i = 1; i <= n; i++) cin >> a[i], Union(a[i], i);
    for (int i = 1; i <= n; i++) ans[find(i)]++;
    for (int i = 1; i <= n; i++)
      printf("%d%c", ans[find(i)], i == n ? '\n' : ' ');
  }
}
