#include <bits/stdc++.h>
using namespace std;
long long fac[1000001];
long long prime[1000000];
long long par[100005];
long long rank1[100005];
void fact() {
  fac[0] = 1;
  for (int i = 1; i <= 10000000; i++) {
    fac[i] = (fac[i - 1] * i) % 1000000007;
  }
  return;
}
long long power(long long n, long long p) {
  if (p == 0) return 1;
  long long res = power((n * n) % 1000000007, p / 2);
  if (p % 2 == 1) res = (n * res) % 1000000007;
  return res;
}
long long SC(long long N) {
  long long P = (N + 1) % 1000000007;
  long long C = (N * P) % 1000000007;
  long long Q = (C * 500000004) % 1000000007;
  return Q;
}
void sieve() {
  prime[0] = 0;
  prime[1] = 0;
  for (int i = 2; i < 100000; i++) {
    prime[i] = 1;
  }
  for (int i = 2; i * i < 1000000; i++) {
    if (prime[i] == 1) {
      for (int j = i * i; j < 1000000; j += i) {
        prime[j] = 0;
      }
    }
  }
}
void matrix_multiply(long long** a, long long** b, long long n) {
  long long temp[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; i++) {
      for (int k = 0; k < n; k++) {
        temp[i][j] =
            (temp[i][j] +
             (a[i][k] % 1000000007 * b[k][j] % 1000000007 + 1000000007) %
                 1000000007) %
            1000000007;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      a[i][j] = temp[i][j];
    }
  }
  return;
}
void maxtrix_power(long long** a, long long p, long long n, long long a1,
                   long long b) {
  while (p > 0) {
    matrix_multiply(a, a, n);
    if ((p & 1) == 1) {
      long long** m1 = new long long*[n];
      for (int i = 0; i < n; i++) {
        m1[i] = new long long[n]();
      }
      m1[0][0] = a1;
      m1[0][1] = -b;
      m1[1][0] = 1;
      m1[1][1] = 0;
      matrix_multiply(a, m1, n);
    }
    cout << p << endl;
    p << 1;
  }
  return;
}
long long n_c_r(long long n, long long r) {
  if (n < r) return 0;
  int i = 0;
  long long res = 1;
  if (r < (n / 2)) r = n - r;
  for (i = 0; i < r; i++) {
    res *= (n - i);
    res /= (i + 1);
  }
  return res;
}
void buildtree(long long* tree, long long* arr, long long left, long long right,
               long long treenode) {
  if (left == right) {
    tree[treenode] = arr[left];
    return;
  }
  int mid = (left + right) / 2;
  buildtree(tree, arr, left, mid, 2 * treenode);
  buildtree(tree, arr, mid + 1, right, 2 * treenode + 1);
  tree[treenode] = max(tree[2 * treenode], tree[2 * treenode + 1]);
}
long long query(long long* tree, long long left, long long right,
                long long start, long long end, long long treenode) {
  if (start > right || end < left)
    return -1;
  else if (left >= start && right <= end)
    return tree[treenode];
  else {
    int mid = (left + right) / 2;
    return max(query(tree, left, mid, start, end, 2 * treenode),
               query(tree, mid + 1, right, start, end, 2 * treenode + 1));
  }
}
long long hlp(long long* a, long long p) {
  long long s = 0, e = 1000000;
  long long ans = -1;
  while (s <= e) {
    long long mid = (s + e) / 2;
    if (a[mid] <= p) {
      ans = mid;
      s = mid + 1;
    } else
      e = mid - 1;
  }
  return ans;
}
long long find(long long u) {
  if (par[u] == u) return u;
  return find(par[u]);
}
void union1(long long u, long long v) {
  if (u == v) return;
  long long p1 = find(u);
  long long p2 = find(v);
  if (p1 == p2) return;
  if (rank1[p1] > rank1[p2]) {
    par[v] = u;
    rank1[p1] += rank1[p2];
  } else if (rank1[p1] < rank1[p2]) {
    par[u] = v;
    rank1[p2] += rank1[p1];
  } else {
    par[u] = v;
    rank1[p2] += rank1[p1];
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  long long q;
  cin >> q;
  while (q--) {
    long long n;
    cin >> n;
    long long* a = new long long[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      a[i] = a[i] - 1;
    }
    long long* ans = new long long[n];
    bool* visited = new bool[n]();
    for (int i = 0; i < n; i++) {
      if (visited[i] == false) {
        set<long long> s;
        visited[i] = true;
        if (a[i] == (i)) {
          ans[i] = 1;
        } else {
          s.insert(a[i]);
          long long s1 = a[i];
          while (!visited[s1] && a[s1] != (i)) {
            s.insert(a[s1]);
            visited[s1] = true;
            s1 = a[s1];
          }
          s.insert(a[s1]);
          visited[s1] = true;
          for (auto j : s) {
            ans[j] = s.size();
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      cout << ans[i] << " ";
    }
    cout << endl;
  }
}
