#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9 + 7;
const int MAX = (2e5) + 5;
const int INF = 1e7;
const int N = MAX;
int parent[N];
int ranks[N] = {0};
int freq[N] = {0};
int size[N] = {0};
std::ifstream in("input.txt");
std::ofstream out("output.txt");
vector<long long> vec(MAX), del(MAX), pt(MAX);
multiset<long long> ms;
map<long long, long long> mp;
unordered_map<long long, long long> ump;
vector<pair<long long, long long> > vp;
set<pair<long long, long long> > se;
unordered_set<long long> use;
long long find_set_pc(long long v) {
  if (v == parent[v]) return v;
  return parent[v] = find_set_pc(parent[v]);
}
void make_set_ubr(long long v) {
  parent[v] = v;
  ranks[v] = 0;
  size[v] = 1;
}
void union_sets_ubr(long long a, long long b) {
  a = find_set_pc(a);
  b = find_set_pc(b);
  if (a != b) {
    if (ranks[a] < ranks[b]) swap(a, b);
    parent[b] = a;
    if (ranks[a] == ranks[b]) ranks[a]++;
  }
}
void union_sets_ubs(long long a, long long b) {
  a = find_set_pc(a);
  b = find_set_pc(b);
  if (a != b) {
    if (size[a] < size[b]) swap(a, b);
    parent[b] = a;
    size[a] += size[b];
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, m, u, v, q, x, y, l1, l2, d;
  cin >> q;
  while (q--) {
    cin >> n;
    for (long long i = 0; i <= n; ++i) make_set_ubr(i);
    for (long long i = 1; i <= n; ++i) {
      cin >> d;
      union_sets_ubs(d, i);
    }
    for (long long i = 1; i <= n; ++i) {
      cout << size[find_set_pc(i)] << " ";
    }
    cout << endl;
  }
  return 0;
}
