#include <bits/stdc++.h>
using namespace std;
int a[200005], res[200005];
int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
    }
    memset(res, 0, sizeof(res));
    for (int i = 1; i <= n;) {
      int j = a[i], k = 1;
      while (j != i) {
        j = a[j];
        k++;
      }
      j = a[i];
      res[i] = k;
      while (j != i) {
        res[j] = k;
        j = a[j];
      }
      while (res[i] != 0 && i <= n) i++;
    }
    for (int i = 1; i <= n; i++) printf("%d%c", res[i], i == n ? '\n' : ' ');
  }
  return 0;
}
