#include <bits/stdc++.h>
using namespace std;
const int maxn = 220;
int n;
int fa[maxn];
int ans[maxn];
int cnt;
int find(int i) {
  cnt++;
  if (fa[i] == i)
    return i;
  else
    return find(fa[i]);
}
int setans(int i, int p) {
  ans[i] = p;
  if (fa[i] == i)
    return i;
  else
    return setans(fa[i], p);
}
void init() {
  for (int i = 1; i < maxn; i++) {
    fa[i] = i;
  }
  for (int i = 1; i < maxn; i++) {
    ans[i] = 1;
  }
}
int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &n);
    init();
    for (int i = 1; i <= n; i++) {
      int x;
      scanf("%d", &x);
      cnt = 0;
      if (find(x) == i) {
        setans(x, cnt);
      } else
        fa[i] = x;
    }
    for (int i = 1; i <= n; i++) {
      cout << ans[i] << " ";
    }
    if (q != 0) cout << endl;
  }
  return 0;
}
