#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, count, init, i, neg, temp;
    cin >> n;
    vector<int> v(n);
    vector<int> v1(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    for (int j = 0; j < n; j++) {
      count = 1;
      i = j;
      if (v[j] == 0) continue;
      while (1) {
        if (v[i] > v.size()) {
          break;
        }
        if (v[i] == j + 1) {
          break;
        }
        i = v[i] - 1;
        count++;
      }
      i = j;
      neg = count;
      while (neg > 0) {
        v1[i] = count;
        temp = v[i] - 1;
        v[i] = 0;
        i = temp;
        neg--;
      }
    }
    for (int i = 0; i < n; i++) {
      cout << v1[i] << " ";
    }
    cout << endl;
  }
  return 0;
}
