#include <bits/stdc++.h>
using namespace std;
double const eps = 1e-11;
double const pi = 3.1415926535;
long long const mod = 1e9 + 7;
int a[205];
int p[205];
int b[205];
int c[205];
void solve() {
  for (int i = 1; i <= 202; i++) a[i] = i;
  for (int i = 1; i <= 202; i++) b[i] = i;
  for (int i = 1; i <= 202; i++) c[i] = 0;
  int n;
  cin >> n;
  bool flag = 0;
  for (int i = 1; i <= n; i++) cin >> p[i];
  int k = 1;
  for (int i = 1; i <= n; i++)
    if (p[i] == i) c[i] == 1;
  while (flag == 0) {
    for (int i = 1; i <= n; i++) a[p[i]] = b[i];
    for (int i = 1; i <= n; i++) b[i] = a[i];
    for (int i = 1; i <= n; i++)
      if (a[i] == i && c[i] == 0) c[i] = k;
    flag = 1;
    for (int i = 1; i <= n; i++)
      if (c[i] == 0) flag = 0;
    k++;
  }
  for (int i = 1; i <= n; i++) cout << c[i] << " ";
  cout << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  while (q--) solve();
  return 0;
}
