#include <bits/stdc++.h>
using namespace std;
const int N = 2e2 + 5;
int t;
int fa[N], n, x;
int ans[N];
int forf(int x) { return fa[x] == x ? x : fa[x] = forf(fa[x]); }
int main() {
  while (cin >> t) {
    while (t--) {
      memset(ans, 0, sizeof ans);
      cin >> n;
      for (register int i = 0; i <= n; ++i) fa[i] = i;
      for (register int i = 1; i <= n; ++i) {
        scanf("%d", &x);
        int t1 = forf(x), t2 = forf(i);
        if (t1 > t2) swap(t1, t2);
        if (t1 != t2) fa[t1] = t2;
      }
      for (register int i = 1; i <= n; ++i) fa[i] = forf(i);
      for (register int i = 1; i <= n; ++i) ans[fa[i]]++;
      for (register int i = 1; i <= n; ++i) cout << ans[fa[i]] << " ";
      puts("");
    }
  }
  return 0;
}
