#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn], ans[maxn];
int t, n;
inline int read() {
  int k = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    k = k * 10 + c - '0';
    c = getchar();
  }
  return k * f;
}
int main() {
  ios::sync_with_stdio(false);
  t = read();
  while (t--) {
    n = read();
    memset(ans, 0, sizeof(ans));
    for (int i = 1; i <= n; i++) a[i] = read();
    for (int i = 1; i <= n; i++) {
      if (ans[i]) continue;
      int cnt = 1;
      int p = i;
      while (i != a[p]) {
        p = a[p], cnt++;
      }
      ans[i] = cnt;
      p = a[i];
      while (p != i) {
        ans[p] = cnt;
        p = a[p];
      }
    }
    for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
    cout << endl;
  }
  return 0;
}
