#include <bits/stdc++.h>
using namespace std;
vector<int> b;
vector<int> p, a;
void f(int ind, int day) {
  if (!b[ind]) {
    a[ind] = day;
    return;
  }
  b[ind] = false;
  f(p[ind] - 1, day + 1);
}
int main() {
  long long q, n;
  cin >> q;
  while (q--) {
    cin >> n;
    p = vector<int>(n);
    a = vector<int>(n, 0);
    b = vector<int>(n, 1);
    for (auto &i : p) cin >> i;
    for (int i = 0; i < n; i++) {
      f(i, a[i]);
      b = vector<int>(n, 1);
    }
    for (int i = 0; i < n; i++) cout << a[i] << ' ';
    cout << endl;
  }
}
