#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    int a[4][n], o[n];
    for (int i = 0; i < n; i++) {
      cin >> a[0][i];
      a[1][i] = i + 1;
      a[2][i] = a[0][i];
      a[3][i] = 0;
    }
    int c = 1, d = 1;
    for (int i = 0; i < n; i++) {
      if ((a[2][i] == i + 1) && (a[3][i] == 0)) {
        o[i] = 1;
        a[3][i] = 1;
        c++;
      }
    }
    while (c < n) {
      d++;
      for (int i = 0; i < n; i++) {
        a[1][i] = a[2][i];
      }
      for (int i = 0; i < n; i++) {
        a[2][i] = a[1][a[0][i] - 1];
        if ((a[2][i] == i + 1) && (a[3][i] == 0)) {
          o[i] = d;
          a[3][i] = 1;
          c++;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      cout << o[i] << " ";
    }
    cout << "\n";
  }
}
