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 
//package Div3;

import java.awt.List;
import java.io.*;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.HashMap;
import java.util.TreeSet;
import java.util.TreeMap;
import java.util.Arrays;
import java.util.Collections;

public class Submission {
	static int MOD = (int) 1e9 + 7;
	static boolean visited[];
	static ArrayList<Integer> adjList[];
	static int count;
	static ArrayList<Integer> list;

	public static void main(String[] args) throws IOException, InterruptedException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int q = sc.nextInt();
		while (q-- > 0) {
			int n = sc.nextInt();
			adjList = new ArrayList[n + 1];
			for (int i = 0; i <= n; i++)
				adjList[i] = new ArrayList<>();
			for (int i = 1; i <= n; i++)
				adjList[i].add(sc.nextInt());
			visited = new boolean[n + 1];
			list = new ArrayList<>();
			int ans[] = new int[n + 1];
			for (int i = 1; i <= n; i++)
				if (!visited[i]) {
					count = 0;
					list = new ArrayList<>();
					dfs(i);
					for (int p : list)
						ans[p] = list.size();
				}
			for (int i = 1; i <= n; i++)
				pw.print(ans[i] + " ");
			pw.println();
		}
		pw.flush();
	}

	static void dfs(int p) {
		visited[p] = true;
		count++;
		list.add(p);
		for (int v : adjList[p])
			if (!visited[v])
				dfs(v);
	}

	static class UnionFind {
		int[] p, rank, setSize;
		int numSets;

		public UnionFind(int N) {
			p = new int[numSets = N];
			rank = new int[N];
			setSize = new int[N];
			for (int i = 0; i < N; i++) {
				p[i] = i;
				setSize[i] = 1;
			}
		}

		public int findSet(int i) {
			return p[i] == i ? i : (p[i] = findSet(p[i]));
		}

		public boolean isSameSet(int i, int j) {
			return findSet(i) == findSet(j);
		}

		public void unionSet(int i, int j) {
			if (isSameSet(i, j))
				return;
			numSets--;
			int x = findSet(i), y = findSet(j);
			if (rank[x] > rank[y]) {
				p[y] = x;
				setSize[x] += setSize[y];
			} else {
				p[x] = y;
				setSize[y] += setSize[x];
				if (rank[x] == rank[y])
					rank[y]++;
			}
		}
	}

	static int findMax(int left, int right) {

		if (left > right)
			return -1;

		int mid = (left + right) / 2;

		if (right - left == 1) {
			return Math.max(ar[left], ar[right]);
		} else if (right == left) {
			return ar[left];
		}

		return Math.max(findMax(left, mid), findMax(mid + 1, right));

	}

	static int findMin(int left, int right) {

		if (left > right)
			return -1;

		int mid = (left + right) / 2;

		if (right - left == 1) {
			return Math.min(ar[left], ar[right]);
		} else if (right == left) {
			return ar[left];
		}

		return Math.min(findMin(left, mid), findMin(mid + 1, right));

	}

	static int ar[] = { 3, 4, 5, 1 };

	static void sort(int[] a) {
		shuffle(a);
		Arrays.sort(a);
	}

	static void shuffle(int[] a) {
		int n = a.length;
		Random rand = new Random();
		for (int i = 0; i < n; i++) {
			int tmpIdx = rand.nextInt(n);
			int tmp = a[i];
			a[i] = a[tmpIdx];
			a[tmpIdx] = tmp;
		}
	}

	static long pow(long a, int n) {
		long res = 1;
		while (n != 0) {
			if ((n & 1) == 1) {
				res = (res * a) % MOD;
			}
			a = (a * a) % MOD;
			n >>= 1;
		}
		return res;
	}

	static long powMOD(long a, long n) {
		long res = 1;
		while (n != 0) {
			if ((n & 1) == 1) {
				res = (res * a) % MOD;
			}
			a = (a * a) % MOD;
			n >>= 1;
		}
		return res;
	}

	static ArrayList<Integer> primes; // generated by sieve
	static int[] isComposite;

	static void sieve(int N) // O(N log log N)
	{
		isComposite = new int[N + 1];
		isComposite[0] = isComposite[1] = 1; // 0 indicates a prime number
		primes = new ArrayList<Integer>();

		for (int i = 2; i <= N; ++i) // can loop till i*i <= N if primes array is not needed O(N log log sqrt(N))
			if (isComposite[i] == 0) // can loop in 2 and odd integers for slightly better performance
			{
				primes.add(i);
				if (1l * i * i <= N)
					for (int j = i * i; j <= N; j += i) // j = i * 2 will not affect performance too much, may alter in
														// modified sieve
						isComposite[j] = 1;
			}
	}

	/*
	 * 1. Generating a list of prime factors of N
	 */
	static HashSet<Long> primeFactors(int N) // O(sqrt(N) / ln sqrt(N))
	{
		HashSet<Long> factors = new HashSet<>(); // take abs(N) in case of -ve integers
		int idx = 0, p = primes.get(idx);

		while (p * p <= N) {
			while (N % p == 0) {
				factors.add(p * 1l);
				N /= p;
			}
			p = primes.get(++idx);
		}

		if (N != 1) // last prime factor may be > sqrt(N)
			factors.add(N * 1l); // for integers whose largest prime factor has a power of 1
		return factors;
	}

	static class Pair {
		int from, to;

		Pair(int x, int y) {
			from = x;
			to = y;
		}
	}

	static int lcm(int a, int b) {
		return (a * b) / gcd(a, b);
	}

	static int gcd(int a, int b) {
		if (a == 0)
			return b;
		return gcd(b % a, a);
	}

	static int findGCD(int a[], int n) {
		int result = a[0];
		for (int i = 1; i < n; i++)
			result = gcd(a[i], result);

		return result;
	}

	static int findLCM(int a[], int n) {
		int result = lcm(a[0], a[1]);
		for (int i = 1; i < n; i++)
			result = lcm(result, a[i]);
		return result;
	}

	static class Point {

		long x, y;

		Point(long a, long b) {
			x = a;
			y = b;
		}
	}

	static class Rectangle {

		static final double EPS = 1e-9;

		Point ll, ur;

		Rectangle(Point a, Point b) {
			ll = a;
			ur = b;
		}

		long area() {
			return (ur.x - ll.x) * (ur.y - ll.y);
		}

		boolean contains(Point p) {
			return p.x <= ur.x + EPS && p.x + EPS >= ll.x && p.y <= ur.y + EPS && p.y + EPS >= ll.y;
		}

		Rectangle intersect(Rectangle r) {
			Point ll = new Point(Math.max(this.ll.x, r.ll.x), Math.max(this.ll.y, r.ll.y));
			Point ur = new Point(Math.min(this.ur.x, r.ur.x), Math.min(this.ur.y, r.ur.y));
			if (Math.abs(ur.x - ll.x) > EPS && Math.abs(ur.y - ll.y) > EPS && this.contains(ll) && this.contains(ur)
					&& r.contains(ll) && r.contains(ur))
				return new Rectangle(ll, ur);
			return null;
		}

	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(4000);
		}
	}
}
