#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 2 * 1e5 + 228;
const long long INF = 1e9;
const long long MOD = 998244353;
int q, cnt;
bool used[MAXN];
map<int, int> mp;
vector<vector<int> > g(MAXN);
void dfs(int v) {
  used[v] = true;
  ++cnt;
  if (!used[g[v][0]]) dfs(g[v][0]);
  mp[v] = cnt;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> q;
  while (q--) {
    memset(used, false, sizeof(used));
    int n;
    cin >> n;
    for (int i = 0; i <= n - 1; ++i) {
      int a;
      cin >> a;
      g[i].push_back(a - 1);
    }
    for (int i = 0; i <= n - 1; ++i) {
      if (!used[i]) {
        dfs(i);
        cnt = 0;
      }
      cout << mp[i] << " ";
      g[i].clear();
    }
    cout << "\n";
  }
}
