#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  if (n == 1) {
    int a;
    cin >> a;
    cout << "1"
         << "\n";
    return;
  } else {
    vector<long long> v(n);
    vector<long long> ans(n, 0);
    long long count = 0;
    vector<long long> v1(n);
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      a--;
      v[i] = a;
    }
    for (int i = 0; i < n; i++) {
      v1[i] = i;
    }
    bool ans1 = true;
    while (ans1) {
      count++;
      ans1 = false;
      vector<long long> v2(v1);
      for (int i = 0; i < n; i++) {
        v1[v[i]] = v2[i];
      }
      for (int i = 0; i < n; i++) {
        if (v1[i] == i && ans[i] == 0) {
          ans[i] = count;
        }
      }
      for (int i = 0; i < n; i++) {
        if (ans[i] == 0) {
          ans1 = true;
          break;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      cout << ans[i] << " ";
    }
    cout << "\n";
    return;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    solve();
  }
}
