The only difference between easy and hard versions is constraints.

There are n kids, each of them is reading a unique book. At the end of any day, the i-th kid will give his book to the p_i-th kid (in case of i = p_i the kid will give his book to himself). It is guaranteed that all values of p_i are distinct integers from 1 to n (i.e. p is a permutation). The sequence p doesn't change from day to day, it is fixed.

For example, if n=6 and p=[4, 6, 1, 3, 5, 2] then at the end of the first day the book of the 1-st kid will belong to the 4-th kid, the 2-nd kid will belong to the 6-th kid and so on. At the end of the second day the book of the 1-st kid will belong to the 3-th kid, the 2-nd kid will belong to the 2-th kid and so on.

Your task is to determine the number of the day the book of the i-th child is returned back to him for the first time for every i from 1 to n.

Consider the following example: p = [5, 1, 2, 4, 3]. The book of the 1-st kid will be passed to the following kids:

  * after the 1-st day it will belong to the 5-th kid, 
  * after the 2-nd day it will belong to the 3-rd kid, 
  * after the 3-rd day it will belong to the 2-nd kid, 
  * after the 4-th day it will belong to the 1-st kid. 



So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.

You have to answer q independent queries.

Input

The first line of the input contains one integer q (1 ≤ q ≤ 1000) — the number of queries. Then q queries follow.

The first line of the query contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of kids in the query. The second line of the query contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n, all p_i are distinct, i.e. p is a permutation), where p_i is the kid which will get the book of the i-th kid.

It is guaranteed that ∑ n ≤ 2 ⋅ 10^5 (sum of n over all queries does not exceed 2 ⋅ 10^5).

Output

For each query, print the answer on it: n integers a_1, a_2, ..., a_n, where a_i is the number of the day the book of the i-th child is returned back to him for the first time in this query.

Example

Input


6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3


Output


1 1 1 1 1 
3 3 3 
2 3 3 2 1 3 
1 
2 2 2 2 
4 4 4 1 4 
import java.util.Scanner;


public class BooksExchangeHardVersion_1249B2 {
    private static Scanner sc = new Scanner(System.in);

    /**
     * Union Find Data structure
     * https://github.com/love1024/Algorithms-Library-In-Java/blob/master/src/UnionFind/UnionFind.java
     */
    public class UnionFind {
        private int[] parent;
        private int[] sizes;

        public UnionFind(int N) {
            // Set parent of each element to itself
            // (N array accesses)
            parent = new int[N];
            sizes = new int[N];

            for (int i=0; i<N; i++) {
                parent[i] = i;
                sizes[i] = 1;
            }
        }

        int root(int p) {
            // Check parent pointers until reach root
            // (Depth of i array accesses)
            while (parent[p] != p) {

                //PATH COMPRESSION
                //No need to traverse path all time
                //Just make it point to grandparent instead of parent
                //Halving the length to traverse, as we only care about root
                parent[p] = parent[parent[p]];

                p = parent[p];
            }
            return p;
        }

        public boolean connected(int p, int q) {
            // Check if p and q have the same root
            // (Depth of p and q array accesses)
            return this.root(p) == this.root(q);
        }

        public void union(int p, int q) {
            // Change root of p to point to q
            // (Depth of p and q array accesses)
            int p_root = this.root(p);
            int q_root = this.root(q);

            // If they already under same parent, no need to do anything
            if(p_root == q_root) return;

            // If size of p is small, attach it to q, otherwise opposite
            if(sizes[p_root] < sizes[q_root]) {
                this.parent[p_root] = q_root;
                sizes[q_root] += sizes[p_root];
            } else {
                this.parent[q_root] = p_root;
                sizes[p_root] += sizes[q_root];
            }
        }

        // Get size of the root element
        public int getRootSize(int p) {
            int root = this.root(p);
            return this.sizes[root];
        }
    }


    public static void main(String[] args) {
        // Read number of queries
        int q = sc.nextInt();

        // While there are some queries left
        while(q-- > 0) {

            // Read number of kids and store them in an array
            int n = sc.nextInt();

            // Union find data structures to union elements that are cyclic or
            // one element passes through components before reaching to its origin
            UnionFind uf = new BooksExchangeHardVersion_1249B2().new UnionFind(n);
            int[] kids = new int[n];
            for(int i = 0; i < n; i++) {
                kids[i] = sc.nextInt();

                // Union current one and the element it is pointing to
                uf.union(i, kids[i] - 1);
            }

            // Loop over all kids
            for(int i = 0; i < n; i++) {

                // Get size of the connected components with the current element
                int size = uf.getRootSize(kids[i]-1);

                // Print number of days it took to reach the current element again or
                // size of the connected components
                System.out.print(size + (i == n -1 ? "" : " "));
            }
            System.out.println("");
        }
    }

}
