#include <bits/stdc++.h>
using namespace std;
int n, ans = 0, vis[210] = {}, p[210];
vector<int> v[210];
void dfs(int node, int &cnt) {
  vis[node]++;
  cnt++;
  for (int i = 0; i < v[node].size(); i++)
    if (!vis[v[node][i]]) {
      dfs(v[node][i], cnt);
    }
  p[node] = cnt;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int a[205] = {};
    cin >> n;
    for (int i = 1; i <= n; i++) v[i].clear(), vis[i] = 0, cin >> a[i];
    for (int i = 1; i <= n; i++) {
      int tmp = i, tmp2 = 0;
      while (a[tmp] != i && a[tmp2] != a[tmp]) {
        if (!tmp2) tmp2 = tmp;
        v[a[tmp]].push_back(i);
        v[i].push_back(a[tmp]);
        tmp = a[tmp];
      }
    }
    for (int i = 1; i <= n; i++) {
      if (!vis[a[i]]) {
        int cnt = 0;
        dfs(a[i], cnt);
      }
      cout << p[a[i]] << ' ';
    }
    cout << '\n';
  }
  return 0;
}
