#include <bits/stdc++.h>
using namespace std;
const int ms = 2e5 + 2;
static int cas = 1;
bool check[ms];
int t, n, parent[ms], cost[ms], arr[ms];
int main() {
  scanf("%d", &t);
  while (t--) {
    memset(check, true, sizeof(check));
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
    for (int i = 1; i <= n; i++)
      if (check[arr[i]]) {
        int path = 0, temp = arr[i];
        do {
          path++;
          parent[temp] = arr[i];
          check[temp] = false;
          temp = arr[temp];
        } while (temp != arr[i]);
        cost[temp] = path;
      }
    for (int i = 1; i <= n; i++) cout << cost[parent[arr[i]]] << ' ';
    cout << '\n';
  }
  return 0;
}
