#include <bits/stdc++.h>
using namespace std;
const int maxn = 205;
int T, n, t;
int a[maxn], mark[maxn], p[maxn];
template <class T>
int read(T &res) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF) return 0;
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  sgn = (c == '-') ? -1 : 1;
  res = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), (c >= '0' && c <= '9')) res = res * 10 + (c - '0');
  res *= sgn;
  return 1;
}
void write(int x) {
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
int findPath(int x) {
  int ans = 0, i = x;
  while (true) {
    ans++;
    mark[i] = x;
    i = a[i];
    if (i == x) break;
  }
  mark[x] = ans;
  return ans;
}
int main() {
  cin >> T;
  while (T--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      read(a[i]);
    }
    memset(mark, 0, sizeof(mark));
    for (int i = 1; i <= n; i++) {
      if (!mark[i])
        write(findPath(i));
      else
        write(mark[mark[i]]);
      if (i != n) putchar(' ');
    }
    putchar('\n');
  }
  return 0;
}
