#include <string>
#include <set>
#include <iostream>
#include <memory.h>
#include <stack>
#include <vector>
using namespace std;

struct S {
	int c,k;
	vector <int> v;
};
S A(int c,int k,vector <int> v) {
	S res;
	res.c=c;
	res.k=k;
	res.v=v;
	return res;
}
int main() {
	vector <int> h;
	h.push_back(0);
	string x;
	int k,a,b;
	cin >> k >> a >> b;
	stack <S> s;
	cin >> x;
	int l=x.length();
	for (int i=a;i<=b;i++)
		if (i<=l) s.push(A(i,1,h));
	S t;
	while (!s.empty()) {
		t=s.top();
		s.pop();
		t.v.push_back(t.c);
		if (t.k>k) continue;
		if (t.c==l && t.k==k) {
			for (int i=1;i<t.v.size();i++) {
				cout << x.substr(t.v[i-1],t.v[i]-t.v[i-1]) << endl;
			}
			//cout << x.substr(t.v.back(),l-t.v.back());
			return 0;
		}

		for (int i=a;i<=b;i++)
			if (t.c+i<=l) s.push(A(t.c+i,t.k+1,t.v));
	}
	cout << "No solution" ;
}


#include <string>
#include <set>
#include <iostream>
#include <memory.h>
#include <stack>
#include <vector>
using namespace std;

struct S {
	int c,k;
	vector <int> v;
};
S A(int c,int k,vector <int> v) {
	S res;
	res.c=c;
	res.k=k;
	res.v=v;
	return res;
}
bool v[222];
int main() {
	memset(v,false,sizeof(v));
	vector <int> h;
	h.push_back(0);
	string x;
	int k,a,b;
	cin >> k >> a >> b;
	stack <S> s;
	cin >> x;
	int l=x.length();
	for (int i=a;i<=b;i++)
		if (i<=l) s.push(A(i,1,h));
	S t;
	while (!s.empty()) {
		t=s.top();
		s.pop();
		t.v.push_back(t.c);
		if (t.k>k || v[t.c]) continue;
		v[t.c]=true;
		if (t.c==l && t.k==k) {
			for (int i=1;i<t.v.size();i++) {
				cout << x.substr(t.v[i-1],t.v[i]-t.v[i-1]) << endl;
			}
			//cout << x.substr(t.v.back(),l-t.v.back());
			return 0;
		}

		for (int i=a;i<=b;i++)
			if (t.c+i<=l) s.push(A(t.c+i,t.k+1,t.v));
	}
	cout << "No solution" ;
}


#include <string>
#include <set>
#include <iostream>
#include <memory.h>
#include <stack>
#include <vector>
using namespace std;

struct S {
	int c,k;
	vector <int> v;
};
S A(int c,int k,vector <int> v) {
	S res;
	res.c=c;
	res.k=k;
	res.v=v;
	return res;
}
bool v[222][222];
int main() {
	memset(v,false,sizeof(v));
	vector <int> h;
	h.push_back(0);
	string x;
	int k,a,b;
	cin >> k >> a >> b;
	stack <S> s;
	cin >> x;
	int l=x.length();
	for (int i=a;i<=b;i++)
		if (i<=l) s.push(A(i,1,h));
	S t;
	while (!s.empty()) {
		t=s.top();
		s.pop();
		t.v.push_back(t.c);
		if (t.k>k || v[t.c][t.k]) continue;
		v[t.c][t.k]=true;
		if (t.c==l && t.k==k) {
			for (int i=1;i<t.v.size();i++) {
				cout << x.substr(t.v[i-1],t.v[i]-t.v[i-1]) << endl;
			}
			//cout << x.substr(t.v.back(),l-t.v.back());
			return 0;
		}

		for (int i=a;i<=b;i++)
			if (t.c+i<=l) s.push(A(t.c+i,t.k+1,t.v));
	}
	cout << "No solution" ;
}


#define _CRT_SECURE_NO_DEPRECATE
#define _USE_MATH_DEFINES

#include <utility>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

int k, a, b, n;
string s;

int main()
{
        //freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
        cin >> k >> a >> b >> s;
        n = (int) s.length();
        if (k*a > n || k*b < n) cout << "No solution";
        else
        {
                int div = n/k;
                int mod = n%k;
                int start = 0;
                for (int i = 0; i < mod; ++i)
                {
                        for (int j = 0; j < div + 1; ++j)
                        {
                                cout << s[start];
                                ++start;
                        }
                        cout << endl;
                }
                for (int i = mod; i < k; ++i)
                {
                        for (int j = 0; j < div; ++j)
                        {
                                cout << s[start];
                                ++start;
                        }
                        cout << endl;
                }
        }
        return 0;
}


#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
using namespace std;

const int NMAX = 201;

int ans[NMAX];
int dp[NMAX][NMAX];

int F(int n, int k, int a, int b)
{
	if (!n && !k)
		return 1;
	if (!n && k < 0)
		return 0;
	if (n < 0)
		return 0;

	if (dp[n][k] == -1)
	{
		dp[n][k] = 0;
		for (int i = a; i <= b; ++i)
		{
			dp[n][k] |= F(n - i, k - 1, a, b);
			if (F(n - i, k - 1, a, b))
				ans[n] = n - i;
		}
	}

	return dp[n][k];
}

string str[NMAX];
int size = 0;

int main()
{
	int k, a, b;
	string s;
	cin >> k >> a >> b >> s;

	memset(dp, 255, sizeof(dp));

	if (!F(s.length(), k, a, b))
	{
		cout << "No solution";
		return 0;
	}

	int prev = s.length();
	while (prev != 0)
	{
		str[size++] = s.substr(ans[prev], prev - ans[prev]);
		prev = ans[prev];
	}

	for (int i = size - 1; i >= 0; --i)
		cout << str[i] << endl;

	return 0;
}


#include <iostream>
#include <string>

using namespace std;

int main() {
    int k, a, b, num, pls, ptr = 0;
    double tmp;
    string str;

    cin >> k >> a >> b >> str;
    tmp = (static_cast <double> (str.length()) / k);

    if (tmp >= a && tmp <= b) {
        num = tmp;
        pls = str.length() % k;
        for (int i = 0; i < k - pls; ++i) {
            cout << str.substr(ptr, num) << endl;
            ptr += num;
        }
        for (int i = k - pls; i < k; ++i) {
            cout << str.substr(ptr, num + 1) << endl;
            ptr += num + 1;
        }
    }
    else {
        cout << "No solution" << endl;
    }

    return 0;
}


#include <string>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <queue>
#include <iostream>
#include <sstream>
#include <stack>
#include <deque>
#include <cmath>
#include <memory.h>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <utility>
using namespace std;

typedef vector < int > vi;
typedef vector < string > vs;
typedef vector < bool > vb;
#define FOR(i,q,w) for (int i=q;i<w; i++)

string s;
int k,a,b;
bool B[256][256];
vector <string> DP[256][256];
int main()
{
    cin >> k >> a >> b;
    cin >> s;
    FOR (i,0,s.size())
        if (i+1 >= a && i+1 <= b) {
            B[i][1] = 1;
            DP[i][1].push_back(s.substr(0,i+1));
        }
    FOR (i,0,s.size())
        FOR (j,2,k+1)
            if (B[i][j]==false) {
                FOR (l,0,i)
                    if (B[l][j-1] && i-l>=a && i-l<=b) {
                        B[i][j] = true;
                        DP[i][j] = DP[l][j-1];
                        DP[i][j].push_back(s.substr(l+1,i-l));
                        break;
                    }
            }
    if (B[s.size()-1][k]== false) cout << "No solution" << endl;
    else {
        FOR (i,0,DP[s.size()-1][k].size())
            cout << DP[s.size()-1][k][i] << endl;
    }
    cin >> k;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <utility>
#include <stack>
#include <climits>
#include <deque>
#define MAXN 100005
#define MAXM 1000000005
#define MAXLG 18
#define endl '\n'
const double PI = 3.1415926535897932384626433832795;
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
int main() {
	//freopen("input.txt", "r", stdin);//freopen("output.txt","w",stdout);
	ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(12);
	cout << fixed;
	string s; int a,b,k,n,m,r;
	cin>>k>>a>>b>>s;
	n=s.size();
	n-=k*a;
	b -= a;
	if(n<0){
		cout << "No solution" << endl;
		return 0;
	}
	if(n > b * k){
		cout << "No solution" << endl;
		return 0;
	}
	r = n%k, m = n/k;
	for(int i = 0; i < r; ++i){
		cout << s.substr(i*(m+1+a),m+a+1) << endl;
	}
	for(int i = r; i < k; ++i){
		cout << s.substr(r*(m+1+a)+(i-r)*m,m+a) << endl;
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <utility>
#include <stack>
#include <climits>
#include <deque>
#define MAXN 100005
#define MAXM 1000000005
#define MAXLG 18
#define endl '\n'
const double PI = 3.1415926535897932384626433832795;
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
int main() {
	//freopen("input.txt", "r", stdin);//freopen("output.txt","w",stdout);
	ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(12);
	cout << fixed;
	string s; int a,b,k,n,m,r;
	cin>>k>>a>>b>>s;
	n=s.size();
	n-=k*a;
	b -= a;
	if(n<0){
		cout << "No solution" << endl;
		return 0;
	}
	if(n > b * k){
		cout << "No solution" << endl;
		return 0;
	}
	r = n%k, m = n/k;
	for(int i = 0; i < r; ++i){
		cout << s.substr(i*(m+1+a),m+a+1) << endl;
	}
	for(int i = r; i < k; ++i){
		cout << s.substr(r*(m+1+a)+(i-r)*(m+a),m+a) << endl;
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>

using namespace std;

const double eps =  0.0000001;

double res(int x, int y){
    double r=x*x + y*y;
    return sqrt(r);
}

int main(){

    //freopen("chemcode.in", "r", stdin);
    //freopen("chemcode.out", "w", stdout);

    int k,a,b;
    cin >> k >> a >> b;
    string s;
    cin >> s;
    int size=s.size();
    int sum=0;
    vector <int> n(k,a);
    for(int i=0;i<k;++i)
        sum+=n[i];
    bool f=true;
    while(f){
        if(sum!=size){
            int ct=0;
            for(int i=0;i<k;++i)
                if(n[i]==b)
                    ++ct;
            if(ct==k){
                cout << "No solution" << endl;
                return 0;
            }
            for(int j=0;j<k;++j){
                if(n[j]==b)
                    continue;
                else{
                    ++n[j];
                    ++sum;
                    break;
                }
            }
        }
        else
            f=false;
    }
    int ct=0;
    int j=0;
    for(int i=0;i<k;++i){
        for(j;j<s.size();++j){;
            if(ct==n[i]){
                cout << endl;
                ct=0;
                break;
            }
            else{
                cout << s[j];
                ++ct;
            }
        }
    }
    return 0;
}


#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <math.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <deque>
#include <map>
#include <cstdlib>
using namespace std;

// Disable warning messages C4996.
//#pragma warning(disable:4996)

#define INPUT_FROM 0
#if INPUT_FROM
#define fromc from
#else
#define fromc cin
#endif

//int fr(vector<int>&, int);

int main(int argc, char **argv)
{
	ifstream from;
	//const int fin = 0;//0 - cin, 1 - from file
	const int err = 0;//0 - Ok, 1 - find error
	const long long nmin=1, nmax=200;
	long long n, m, res, rt, i, j, k, ax, a, b, c;
	//char ch;
	//string sres[2]={"Masha", "Stas"};
	string s, st, ss, sb;
	//long double dt;

	if(INPUT_FROM) from.open("test.txt");
	fromc>>k>>a>>b;

	//vector<long long> v, va;
	//map<long long, long long> mi;
	//map<long long, vector<long long> > mz;

	fromc>>s;
	n=s.length();

	if((a*k<=n)&&(n<=b*k)){
		c=n/k;
		c+=(n%k==0)?0:1;
		m=0;
		while(m<n){
			st=s.substr(m, c);
			cout<<st<<endl;
			m+=c;
		}
	}else cout<<"No solution"<<endl;

	//cout<<res<<endl;

	return 0;
}


#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <math.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <deque>
#include <map>
#include <cstdlib>
using namespace std;

// Disable warning messages C4996.
//#pragma warning(disable:4996)

#define INPUT_FROM 0
#if INPUT_FROM
#define fromc from
#else
#define fromc cin
#endif

//int fr(vector<int>&, int);

int main(int argc, char **argv)
{
	ifstream from;
	//const int fin = 0;//0 - cin, 1 - from file
	const int err = 0;//0 - Ok, 1 - find error
	const long long nmin=1, nmax=200;
	long long n, m, res, rt, kt, nt, i, j, k, ax, a, b, c;
	//char ch;
	//string sres[2]={"Masha", "Stas"};
	string s, st, ss, sb;
	//long double dt;

	if(INPUT_FROM) from.open("test.txt");
	fromc>>k>>a>>b;

	//vector<long long> v, va;
	//map<long long, long long> mi;
	//map<long long, vector<long long> > mz;

	fromc>>s;
	nt=n=s.length();
	kt=k;
	m=0;
	if((a*k<=n)&&(n<=b*k)){
		while(nt>0){
			c=nt/kt;
			st=s.substr(m, c);
			cout<<st<<endl;
			m+=c;
			nt-=c;
			kt--;
		}
	}else cout<<"No solution"<<endl;

	//cout<<res<<endl;

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<cctype>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<sstream>
#include<vector>
using namespace std;

typedef vector<int> vi;
typedef vector< vi > vii;
typedef map<int,int> mii;
typedef map<string,int> msi;
typedef map<int,string> mis;

#define pb push_back
#define MAX 105
int k,a,b;
string in;

int main(){
//	freopen("a.txt", "r", stdin);
	int i,j,z,v;
	scanf("%d %d %d", &k, &a, &b);
	cin>>in;
	int n = in.size();
	int s = n / k;
	int mod = n % k;
	bool flag = true;
	if( s < a || s > b ) flag = false;
	if( s + 1 > b ) flag = false;

	if( !flag ){
		printf("No solution\n");
		return 0;
	}

	i = 0;
	for(  z = 0; z < s - mod; z ++ ){
		for(  v = 0; v < s; v ++ ){
			printf("%c", in[i]);
			i++;
		}

		printf("\n");
	}

	for(  z = 0; z < mod; z ++ ){
		for(  v = 0; v < s + 1; v ++ ){
			printf("%c", in[i]);
			i++;
		}

		printf("\n");
	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<cctype>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<sstream>
#include<vector>
using namespace std;

typedef vector<int> vi;
typedef vector< vi > vii;
typedef map<int,int> mii;
typedef map<string,int> msi;
typedef map<int,string> mis;

#define pb push_back
#define MAX 105
int k,a,b;
string in;

int main(){
//	freopen("a.txt", "r", stdin);
	int i,j,z,v;
	scanf("%d %d %d", &k, &a, &b);
	cin>>in;
	int n = in.size();
	int s = n / k;
	int mod = n % k;
	bool flag = true;
	if( s < a || s > b ) flag = false;
	if( s + 1 > b && n % k != 0 ) flag = false;

	if( !flag ){
		printf("No solution\n");
		return 0;
	}

	i = 0;
	for(  z = 0; z < k - mod; z ++ ){
		for(  v = 0; v < s; v ++ ){
			printf("%c", in[i]);
			i++;
		}

		printf("\n");
	}

	for(  z = 0; z < mod; z ++ ){
		for(  v = 0; v < s + 1; v ++ ){
			printf("%c", in[i]);
			i++;
		}

		printf("\n");
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int mas[200];

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int k, a, b;
	string s;
	cin >> k >> a >> b;
	cin >> s;
	if (k * a > s.size() || k * b < s.size()) {
		cout << "No solution";
		return 0;
	} else {
		for (int i = 1; i <= k; i++) {
			mas[i] = a;
		}
		int len = s.size() - k * a;
		int num = 1;
		while (len != 0) {
			if (mas[num] < b) {
				mas[num]++;
				len--;
			} else {
				num++;
			}
		}

		int delta = 0;
		for (int i = 1; i <= k; i++) {
			for (int j = delta; j < delta + mas[i]; j++) {
				cout << s[j];
			}
			delta += mas[i];
			cout << endl;
		}
	}
}


#include<string>
#include<iostream>
using namespace std;
int main()
{

	int l,a,b;
	cin>>l>>a>>b;
	string s;
	cin>>s;
	int n=s.size();
	if(l*b<n||l*a>n)
		cout<<"No solution";
	else{
	int *arr=new int[l];
	for(int i=0;i<l;i++)
		arr[i]=n/l;
	int r=n%l;
	for(int i=0;i<l;i++)
		if(r==0)
			break;
		else
			{
				arr[i]++;
				r--;
		}

		int j=0;
		for(int i=0;i<n;)
		{

			cout<<s.substr(i,arr[j])<<endl;
			i=min(arr[j]+i,n);
			j++;
	}
}
	//system("pause");
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

#define pb push_back
#define mp make_pair
#define sqr(x) ((x)*(x))

string str;
int k, a, b;
int sr;

vector <int> ans;

bool do_it(int x, int i)
{
	if (x == k)
		return true;

	if (x == k - 1) {
		bool res = str.length() - i >= a && str.length() - i <= b;
		if (res)
			ans.pb(str.length() - i);
		return res;
	}

	int j;
	for (j = sr; j <= b; ++j) {
		if (do_it(x + 1, i + j)) {
			ans.pb(j);
			return true;
		}
	}

	for (j = sr - 1; j >= a; --j) {
		if (do_it(x + 1, i + j)) {
			ans.pb(j);
			return true;
		}
	}

	return false;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	scanf("%d %d %d\n", &k, &a, &b);

	getline(cin, str);

	int len = str.length();

	if ((len / a + (int)(len % a != 0)) < k ||
		(len / b + (int)(len % b != 0)) > k){
			cout << "No solution";
			return 0;
	}

	sr = len / k;
	sr = max(sr, a);
	sr = min(sr, b);

	if (do_it(0, 0)) {

		int i, j = 0;
		for (i = ans.size() - 1; i >= 0; --i) {
			cout << str.substr(j, min(ans[i], (int)str.length() - i)) << endl;
			j += ans[i];
		}

	} else {
		cout << "No solution";
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

#define pb push_back
#define mp make_pair
#define sqr(x) ((x)*(x))

string str;
int k, a, b;
int sr;

vector <int> ans;

bool do_it(int x, int i)
{
	if (x == k)
		return true;

	if (x == k - 1) {
		bool res = str.length() - i >= a && str.length() - i <= b;
		if (res)
			ans.pb(str.length() - i);
		return res;
	}

	int j;
	for (j = sr; j <= b; ++j) {
		if (do_it(x + 1, i + j)) {
			ans.pb(j);
			return true;
		}
	}

	for (j = sr - 1; j >= a; --j) {
		if (do_it(x + 1, i + j)) {
			ans.pb(j);
			return true;
		}
	}

	return false;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	scanf("%d %d %d\n", &k, &a, &b);

	getline(cin, str);

	int len = str.length();

	if ((len / a + (int)(len % a != 0)) < k ||
		(len / b + (int)(len % b != 0)) > k){
			cout << "No solution";
			return 0;
	}

	sr = len / k;
	sr = max(sr, a);
	sr = min(sr, b);

	do_it(0, 0);

	int i, j = 0;
	for (i = ans.size() - 1; i >= 0; --i) {
		cout << str.substr(j, min(ans[i], (int)str.length() - i)) << endl;
		j += ans[i];
	}

	return 0;
}


#include<iostream>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n;
        cin >> n;

        int a[n];
        for(int i=1;i<=n;i++)
        cin >> a[i];

        for(int i=1;i<=n;i++)
        {
            int temp=a[i];
            int count=1;
            if(a[temp]==i)
            cout << "1 ";
            else
            {
                while(a[temp]!=i)
                {
                    temp=a[temp];
                    count++;
                }
                cout << count+1 << " ";
            }

        }
        cout << endl;
    }
}


#include<iostream>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n;
        cin >> n;

        int a[n];
        for(int i=1;i<=n;i++)
        cin >> a[i];

        for(int i=1;i<=n;i++)
        {
            int temp=a[i];
            int count=1;

                while(a[temp]!=i)
                {
                    temp=a[temp];
                    count++;
                }
                if(count==2)
                cout << "1 " << " ";
                else
                cout << count+1 << " ";

        }
        cout << endl;
    }
}


#include<iostream>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n;
        cin >> n;

        int a[n];
        for(int i=1;i<=n;i++)
        cin >> a[i];

        for(int i=1;i<=n;i++)
        {
            int temp=a[i];
            int count=0;
            if(a[temp]==i)
            cout << "1 ";
            else
            {
                while(a[temp]!=i)
                {
                    temp=a[temp];
                    count++;
                }
                cout << count << " ";
            }

        }
        cout << endl;
    }
}


#include<iostream>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n;
        cin >> n;

        int a[n];
        for(int i=1;i<=n;i++)
        cin >> a[i];

        for(int i=1;i<=n;i++)
        {
            int temp=a[i];
            int count=1;
            int flag=i;
            if(a[temp]==flag)
            cout << "1 ";
            else
            {
                while(a[temp]!=i)
                {
                    temp=a[temp];
                    count++;
                }
                cout << count+1 << " ";
            }

        }
        cout << endl;
    }
}


#include<iostream>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n;
        cin >> n;

        int a[n];
        for(int i=1;i<=n;i++)
        cin >> a[i];

        for(int i=1;i<=n;i++)
        {
            int temp=a[i];
            int count=1;
            if(a[temp]==i)
            cout << "1 ";
            if(a[temp]==flag)
            cout << "2 ";
            else
            {
                while(a[temp]!=i)
                {
                    temp=a[temp];
                    count++;
                }
                cout << count+1 << " ";
            }

        }
        cout << endl;
    }
}


#include<iostream>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n;
        cin >> n;

        int a[n];
        for(int i=1;i<=n;i++)
        cin >> a[i];

        for(int i=1;i<=n;i++)
        {
            int temp=a[i];
            int count=1;
            if(a[a[i]]==i)
            cout << "1 ";
            else
            {
                while(a[temp]!=i)
                {
                    temp=a[temp];
                    count++;
                }
                cout << count+1 << " ";
            }

        }
        cout << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int q;
    cin>>q;
    for(int i=0;i<q;i++)
    {
        int n;
        cin>>n;
        int p[n+1];
        for(int j=1;j<=n;j++)
        {
            cin>>p[j];
        }
        int a[n+1];
        for(int j=1;j<=n;j++)
        {
            int c=1;
            if(j==p[j])
            {
             c=1;
             a[j]=c;
            }
            else
            {
             int x=j;
             while(x!=p[j])
             {
                 j=p[j];
                 c++;
             }
             j=x;
             a[j]=c;
            }
        }
        for(int j=1;j<=n;j++)
        {
            cout<<a[j]<<" ";
        }
        cout<<"\n";

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int q;
    cin>>q;
    for(int i=0;i<q;i++)
    {
        int n;
        cin>>n;
        int p[n+1];
        for(int j=1;j<=n;j++)
        {
            cin>>p[j];
        }
        int a[n+1];
        for(int j=1;j<=n;j++)
        {
            int c=1;
            if(j==p[j])
             c=1;
             int x=j;
             while(x!=p[j])
             {
                 j=p[j];
                 c++;
             }
             a[j]=c;
        }
        for(int j=1;j<=n;j++)
        {
            cout<<a[j]<<" ";
        }
        cout<<"\n";

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int q;
    cin>>q;
    for(int i=0;i<q;i++)
    {
        int n;
        cin>>n;
        int p[n+1];
        for(int j=1;j<=n;j++)
        {
            cin>>p[j];
        }
        int a[n+1];
        for(int j=1;j<=n;j++)
        {
            int c=1;
            if(j==p[j])
             c=1;
             int x=j;
             while(x!=p[j])
             {
                 j=p[j];
                 c++;
             }
             a[j]=c;
        }
        for(int j=1;j<=n;j++)
        {
            cout<<a[j]<<" ";
        }

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int q;
    cin>>q;
    for(int i=0;i<q;i++)
    {
        int n;
        cin>>n;
        int p[n+1];
        for(int j=1;j<=n;j++)
        {
            cin>>p[j];
        }
        int a[n+1];
        for(int j=1;j<=n;j++)
        {
            int c=1;
            if(j==p[j])
            {
             c=1;
             a[j]=c;
            }
            else
            {
             int x=j;
             while(x!=p[j])
             {
                 j=p[j];
                 c++;
             }
             a[j]=c;
            }
        }
        for(int j=1;j<=n;j++)
        {
            cout<<a[j]<<" ";
        }
        cout<<"\n";

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int q;
    cin>>q;
    for(int i=0;i<q;i++)
    {
        int n;
        cin>>n;
        int p[n+1];
        for(int j=1;j<=n;j++)
        {
            cin>>p[i];
        }
        int a[n+1];
        for(int j=1;j<=n;j++)
        {
            int c=1;
            if(j==p[j])
             c=1;
             int x=j;
             while(x!=p[j])
             {
                 j=p[j];
                 c++;
             }
             a[j]=c;
        }
        for(int j=1;j<=n;j++)
        {
            cout<<a[j]<<" ";
        }

    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int fun(int arr[],int i,int ind,int cnt,int dp[])
{
    if(i==ind)
        {
            dp[ind] = cnt;
        }
    else
    {
        fun(arr,i,arr[ind],cnt+1,dp);
        dp[arr[ind]] = dp[i];
        return dp[arr[ind]];
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
 int q;
 cin>>q;
 while(q--)
 {
     int n;
     cin>>n;
     int arr[n];
     for(int i=0;i<n;i++)
     {
         cin>>arr[i];
         arr[i]--;
     }
     int dp[n+1] = {0};

        for(int i=0;i<n;i++)
        {

            if(dp[i]==0)
            {

                dp[arr[i]]=fun(arr,i,arr[i],1,dp);

            }
        }
        for(int i=0;i<n;i++)
            cout<<dp[i]<<" ";
        cout<<endl;
 }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int fun(int arr[],int i,int ind,int cnt,int dp[])
{
    if(i==ind)
        {
            dp[ind] = cnt;
            return dp[ind];
        }
    else
    {
        fun(arr,i,arr[ind],cnt+1,dp);
        dp[arr[ind]] = dp[i];
        return dp[arr[ind]];
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
 int q;
 cin>>q;
 while(q--)
 {
     int n;
     cin>>n;
     int arr[n];
     for(int i=0;i<n;i++)
     {
         cin>>arr[i];
         arr[i]--;
     }
     int dp[n+1] = {0};

        for(int i=0;i<n;i++)
        {

            if(dp[i]==0)
            {

                dp[arr[i]]=fun(arr,i,arr[i],1,dp);

            }
        }
        for(int i=0;i<n;i++)
            cout<<dp[i]<<" ";
        cout<<endl;
 }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int fun(int arr[],int i,int ind,int cnt,int dp[])
{
    if(i==ind)
        {
            dp[ind] = cnt;
        }
    else
    {
        fun(arr,i,arr[ind],cnt+1,dp);
        dp[arr[ind]] = dp[i];
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
 int q;
 cin>>q;
 while(q--)
 {
     int n;
     cin>>n;
     int arr[n];
     for(int i=0;i<n;i++)
     {
         cin>>arr[i];
         arr[i]--;
     }
     int dp[n+1] = {0};

        for(int i=0;i<n;i++)
        {

            if(dp[i]==0)
            {

                dp[arr[i]]=fun(arr,i,arr[i],1,dp);

            }
        }
        for(int i=0;i<n;i++)
            cout<<dp[i]<<" ";
        cout<<endl;
 }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int fun(int arr[],int i,int ind,int cnt,int dp[])
{
    if(i==ind)
        {
            dp[ind] = cnt;
        }
    else
    {
        fun(arr,i,arr[ind],cnt+1,dp);
        dp[arr[ind]] = dp[i];
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
 int q;
 cin>>q;
 while(q--)
 {
     int n;
     cin>>n;
     int arr[n];
     for(int i=0;i<n;i++)
     {
         cin>>arr[i];
         arr[i]--;
     }
     int dp[n+1] = {0};

        for(int i=0;i<n;i++)
        {
            if(dp[i]!=0)
                cout<<dp[i]<<" ";
            else
            {

                dp[arr[i]]=fun(arr,i,arr[i],1,dp);
                cout<<dp[i]<<" ";
            }
        }
        cout<<endl;
 }

}


#include<bits/stdc++.h>
#define M 1000000007
using namespace std;
typedef long long int ll;
bool comp(pair<ll,ll>a,pair<ll,ll>b)
{
    if(a.first<b.first)
    {
        return true;
    }
    else if(a.first==b.first)
    {
        if(a.second<b.second)
            return true;
        else
            return false;
    }
    else
        return false;
}
int main()
{
 int t;
 cin>>t;
 while(t--)
 {
     int n;
     cin>>n;
     int arr[n];
     for(int i=0;i<n;i++)
     {
         cin>>arr[i];
         arr[i]--;
     }
     for(int i=0;i<n;i++)
     {
         int count=1;
         int temp=i;
         int j=i;
         while(arr[j]!=temp)
         {
             count++;
             j = arr[j];
         }
         cout<<count<<" ";
     }
     cout<<endl;
 }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int q;
	cin >> q;
	while(q--)
	{
		int n;
		cin>> n;
		int arr[n+1];
		for(int i=1; i<=n; i++) cin >> arr[i];
		int dp[1001][1001]={0};
		//unordered_map<pair<int,int>, int,hash_pair> mp;
		vector<int> ans;
		for(int i=1; i<=n; i++)
		{
			int count = 0;
			int j= i;
			do{
				if(j<=1000 && i<= 1000)
				{
					if(dp[j][i]!=0)
					{
						count += dp[j][i];
						break;
					}
				}
				j = arr[j];
				count++;
				if(i<=1000 && j<= 1000) dp[i][j]= count;
			}while(j!=i);
			ans.push_back(count);
		}
		for(int i=0; i<n; i++) cout<<ans[i]<<" ";
		cout<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int q;
	cin >> q;
	while(q--)
	{
		int n;
		cin>> n;
		int arr[n+1];
		for(int i=1; i<=n; i++) cin >> arr[i];
		vector<int> ans;
		for(int i=1; i<=n; i++)
		{
			int count = 0;
			int j= i;
			do{
				j = arr[j];
				count++;
			}while(j!=i);
			ans.push_back(count);
		}
		for(int i=0; i<n; i++) cout<<ans[i]<<" ";
		cout<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int q;
	cin >> q;
	while(q--)
	{
		int n;
		cin>> n;
		int arr[n+1];
		for(int i=1; i<=n; i++) cin >> arr[i];
		int dp[10001][10001]={0};
		//unordered_map<pair<int,int>, int,hash_pair> mp;
		vector<int> ans;
		for(int i=1; i<=n; i++)
		{
			int count = 0;
			int j= i;
			do{
				if(j<=10000 && i<= 10000)
				{
					if(dp[j][i]!=0)
					{
						count += dp[j][i];
						break;
					}
				}
				j = arr[j];
				count++;
				if(i<=10000 && j<= 10000) dp[i][j]= count;
			}while(j!=i);
			ans.push_back(count);
		}
		for(int i=0; i<n; i++) cout<<ans[i]<<" ";
		cout<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int q;
	cin >> q;
	while(q--)
	{
		int n;
		cin>> n;
		int arr[n+1];
		for(int i=1; i<=n; i++) cin >> arr[i];
		int dp[n+1][n+1]={0};
		vector<int> ans;
		for(int i=1; i<=n; i++)
		{
			int count = 0;
			int j= i;
			do{
				if(dp[j][i]!=0)
				{
					count += dp[j][i];
					break;
				}
				j = arr[j];
				count++;
				dp[i][j]= count;
			}while(j!=i);
			ans.push_back(count);
		}
		for(int i=0; i<n; i++) cout<<ans[i]<<" ";
		cout<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int q;
	cin >> q;
	while(q--)
	{
		int n;
		cin>> n;
		int arr[n+1];
		for(int i=1; i<=n; i++) cin >> arr[i];
		//int dp[n+1][n+1]={0};
		map<pair<int,int>, int> mp;
		vector<int> ans;
		for(int i=1; i<=n; i++)
		{
			int count = 0;
			int j= i;
			do{
				if(mp[{j,i}]!=0)
				{
					count += mp[{j,i}];
					break;
				}
				j = arr[j];
				count++;
				mp[{i,j}]= count;
			}while(j!=i);
			ans.push_back(count);
		}
		for(int i=0; i<n; i++) cout<<ans[i]<<" ";
		cout<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int q;
	cin >> q;
	while(q--)
	{
		int n;
		cin>> n;
		int arr[n+1];
		for(int i=1; i<=n; i++) cin >> arr[i];
		//int dp[n+1][n+1]={0};
		unordered_map<pair<int,int>, int,hash_pair> mp;
		vector<int> ans;
		for(int i=1; i<=n; i++)
		{
			int count = 0;
			int j= i;
			do{
				if(mp[{j,i}]!=0)
				{
					count += mp[{j,i}];
					break;
				}
				j = arr[j];
				count++;
				mp[{i,j}]= count;
			}while(j!=i);
			ans.push_back(count);
		}
		for(int i=0; i<n; i++) cout<<ans[i]<<" ";
		cout<<"\n";
	}
	return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int q,w,x,day=0;
    int in[2000000];

    cin>>q;
    for(int i=0;i<q;++i)
    {
        cin>>w;
        for(int j=0;j<w;++j)
        {
            cin>>in[j];
        }
        for(int j=0;j<w;++j)
        {
            x=j;
            x = in[x];
            x-=1;
            day++;
            while(x != j)
            {
                x = in[x];
                x-=1;
                day++;
            }
    cout<<day<<" ";
    day=0;
    }
    cout<<"\n";
}
return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int q,w,x,day=1;
    int in[2000000];

    cin>>q;
    for(int i=0;i<q;++i)
    {
        cin>>w;
        for(int j=0;j<w;++j)
        {
            cin>>in[j];
        }
        for(int j=0;j<w;++j)
        {
            x=j;
            x = in[x];
            x-=1;
            day++;
            while(x != j)
            {
                x = in[x];
                x-=1;
                day++;
            }
    cout<<day<<" ";
    day=0;
    }
    cout<<"\n";
}
return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int q,w,x,day=1;
    int in[2000000];

    cin>>q;
    for(int i=0;i<q;++i)
    {
        cin>>w;
        for(int j=0;j<w;++j)
        {
            cin>>in[j];
        }
        for(int j=0;j<w;++j)
        {
            x=w+1;
            while(x != j)
            {
                x = in[x];
                x-=1;
                day++;
            }
    cout<<day<<" ";
    day=0;
    }
    cout<<"\n";
}
return 0;
}


#include <bits/stdc++.h>

using namespace std;

int p[210], cnt[210];

void solve(){
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> p[i];
    for(int i = 1; i <= n; i++){
        int num = i, count = 1;
        int perm = p[i];
        while(perm != num){
            perm = p[perm]; count++;
        }
        cnt[i] = count;
    }
    for(int i = 1; i <= n; i++) cout << cnt[i] << ' ';
    cout << endl;
}

int main(){
    int T;
    cin >> T;
    while(T--){
        solve();
    }
}


#include <bits/stdc++.h>

using namespace std;

int p[200200], ans[200200];
int n;

void solve() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        ans[i] = -1;
    }
    for (int i = 1; i <= n; i++) {
        if (ans[i] == -1) {
            vector<int> path;
            path.push_back(i);
            int it = p[i], len = 1;
            while (it != i) {
                path.push_back(it);
                it = p[it];
                len++;
            }
            for (int u : path) ans[u] = len;
        }
    }
    for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
    cout << '\n';
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}


#include<stdio.h>
int main()
{
	int q,i,j,n,temp,step=1;
	int p[205];
	scanf("%d", &q);
	for(i=0;i<q;i++){
		scanf("%d", &n);
		for(j=0;j<n;j++){
			scanf("%d", &p[j]);
		}
		if(n==1){
			printf("1\n");
		}
		else{
			for(j=1;j<=n;j++){
				temp=p[j];
				if(temp==j){
					printf("%d\n",step);
				}
				else{
					while(temp!=j){
						temp=p[temp];
						step++;
					}
					printf("%d\n", step);
				}
				step=1;
			}
		}
	}
	return 0;
}


#include<stdio.h>
int main()
{
	int q,i,j,n,temp,step=1;
	int p[205];
	scanf("%d", &q);
	for(i=0;i<q;i++){
		scanf("%d", &n);
		for(j=0;j<n;j++){
			scanf("%d", &p[j]);
		}
		if(n==1){
			printf("1\n");
		}
		else{
			for(j=1;j<=n;j++){
				temp=p[j-1];
				if(temp==j){
					printf("%d\n",step);
				}
				else{
					while(temp!=j){
						temp=p[temp];
						step++;
					}
					printf("%d\n", step);
				}
				step=1;
			}
		}
	}
	return 0;
}


#include<stdio.h>
int main()
{
	int q,i,j,n,temp,step=1;
	int p[205];
	scanf("%d", &q);
	for(i=0;i<q;i++){
		scanf("%d", &n);
		for(j=0;j<n;j++){
			scanf("%d", &p[j]);
		}
		if(n==1){
			printf("1\n");
		}
		else{
			for(j=1;j<=n;j++){
				temp=p[j-1];
				if(temp==j){
					printf("%d\n",step);
				}
				else{
					while(temp!=j){
						temp=p[temp-1];
						step++;
					}
					printf("%d\n", step);
				}
				step=1;
			}
		}
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <set>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <array>
#include <deque>
#include <bitset>
typedef long long ll;
using namespace std;
#define trace(x) cerr << #x << " is " << x << endl;

vector<int> solve(vector<int>& a) {
    int n = a.size() - 1;
    vector<int> res;
    for (int i = 1; i <= n; i++) {
        int cur = i;
        int day = 0;
        do {
            cur = a[cur];
            day++;
        } while(cur != i);
        res.push_back(day);
    }
    return res;
}

int main() {
    int q, n;
    cin>>q;
    while(q--) {
        cin >> n;
        vector<int> a(n + 1);
        for (int i = 1; i <= n;i++) {
            cin >> a[i];
        }
        vector<int> res = solve(a);
        for (int i = 0; i < res.size(); i++) {
            if (i > 0) cout << " ";
            cout << res[i];
        }
        cout << endl;
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <set>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <array>
#include <deque>
#include <bitset>
typedef long long ll;
using namespace std;
#define trace(x) cerr << #x << " is " << x << endl;

// #define _DEBUG 1
int main() {
    #ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif
    int q, n;
    cin>>q;
    while(q--) {
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n;i++) {
            cin >> a[i];
            a[i]--;
        }
        for (int i = 0; i < n; i++) {
            int cur = i;
            int cnt = 0;
            do {
                cnt++;
                cur = a[cur];
            }while(cur != i);
            cout << cnt << " ";
        }
        cout << endl;
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <set>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <array>
#include <deque>
#include <bitset>
typedef long long ll;
using namespace std;
#define trace(x) cerr << #x << " is " << x << endl;

#define _DEBUG 1
int main() {
    #ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif
    int q, n;
    cin>>q;
    while(q--) {
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n;i++) {
            cin >> a[i];
            a[i]--;
        }
        for (int i = 0; i < n; i++) {
            int cur = i;
            int cnt = 0;
            do {
                cnt++;
                cur = a[cur];
            }while(cur != i);
            cout << cnt << " ";
        }
        cout << endl;
    }
    return 0;
}


 //"Men apart, every man an emperor"!!!

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define repn(i,n)for(ll i=0;i<n;i++)
int main()
{  int q;
cin>>q;
while(q--){
    int n,val,i,ans=1;
    cin>>n;
    int ar[n+1];
    for(i=1; i<=n; i++)
    {   cin>>ar[i]; }

    for(i=1; i<=n; i++)
    { val = ar[i];
        while(val!=i)
        {
            val = ar[val];
            ans++;
        }
        cout<<ans<<" ";
    }
    cout<<endl;
}
return 0;}


 //"Men apart, every man an emperor"!!!

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define repn(i,n)for(ll i=0;i<n;i++)
int main()
{  int q;
cin>>q;
while(q--){
    ll n,ans=1,val;
    cin>>n;
    ll a[n];
    repn(i,n)
    {cin>>a[i];}

    repn(i,n){
         val=a[i];

        while(val!=i){
            val=a[val];
            ans++;
        }
         cout<<ans<<" ";

    }

    cout<<"\n";

}
return 0;}


 //"Men apart, every man an emperor"!!!

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define repn(i,n)for(ll i=0;i<n;i++)
int main()
{  int q;
cin>>q;
while(q--){
    int n,val,i;
    cin>>n;
    int ar[n+1];
    for(i=1; i<=n; i++)
    {   cin>>ar[i]; }

    for(i=1; i<=n; i++)
    {
         val = ar[i];

        int ans=1;
        while(val!=i)
        {
            val = ar[val];
            ans++;
        }
        cout<<ans<<" ";
    }
    cout<<endl;
}
return 0;}


 //"Men apart, every man an emperor"!!!

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define repn(i,n)for(int i=0;i<n;i++)
int main()
{  int q;
cin>>q;
while(q--){
    int n,ans=1,val;
    cin>>n;
    int a[n];
    repn(i,n)
    {cin>>a[i];}

    repn(i,n){
         val=a[i];

        while(val!=i){
            val=a[val];
            ans++;
        }
         cout<<ans<<" ";

    }

    cout<<"\n";

}
return 0;}


#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<map>
#define int long long
using namespace std;
main()
{
	//freopen("input.txt","rt",stdin);
	int q;
	cin>>q;
	while(q--)
	{
		int n;
		cin>>n;
		int arr[n];
		for(int i=0;i<n;i++)
		{
			cin>>arr[i];
		}
		int ans[n] = {0};
		for(int i=0;i<n;i++)
		{
			ans [i] = arr[i];
		}
		int a[n] = {0};
		int p = 1;
		while(1)
		{
			int c =0;
			for(int i=0;i<n;i++)
			{
				if(ans[i] == i+1)
				{
					if(a[i] == 0)
					a[i] = p;
					c++;
				}
			}
			if(c == n)
			{
				break;
			}
			int ans1[n];
			for(int j=0;j<n;j++)
			{
				ans1[j] = ans[arr[j]-1];
				//cout<<ans[j]<<"   ";
			}
			//break;
			//cout<<endl;
			for(int i=0;i<n;i++)
			{
				ans[i] = ans1[i];
			}
			p++;
		}
			for(int i=0;i<n;i++)
			{
				cout<<a[i]<<" ";
			}
			cout<<endl;
	}
}


#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<map>
#define int long long
using namespace std;
main()
{
	 ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	//freopen("input.txt","rt",stdin);
	int q;
	cin>>q;
	while(q--)
	{
		int n;
		cin>>n;
		int arr[n];
		for(int i=0;i<n;i++)
		{
			cin>>arr[i];
		}
		int ans[n] = {0};
		for(int i=0;i<n;i++)
		{
			ans [i] = arr[i];
		}
		int a[n] = {0};
		int p = 1;
		while(1)
		{
			int c =0;
			for(int i=0;i<n;i++)
			{
				if(ans[i] == i+1)
				{
					if(a[i] == 0)
					a[i] = p;
					c++;
				}
			}
			if(c == n)
			{
				break;
			}
			int ans1[n];
			for(int j=0;j<n;j++)
			{
				ans1[j] = ans[arr[j]-1];
			}
//			cout<<"here "<<endl;
//				for(int j=0;j<n;j++)
//			{
//				cout<<ans1[j]<<"   ";
//			}
//			cout<<endl;
			for(int i=0;i<n;i++)
			{
				ans[i] = ans1[i];
			}
			p++;
		}
			for(int i=0;i<n;i++)
			{
				cout<<a[i]<<" ";
			}
			cout<<endl;
	}
}


#include<bits/stdc++.h>
#define mx 1000006
using namespace std;
int dsu[mx];
int sz[mx];
void init(int n)
{
	for(int i=1;i<=n;i++)
	{
		dsu[i] = i;
		sz[i] = 1;
	}
}
int root(int a)
{
	while(dsu[a] != a)
	{
		a = dsu[a];
	}
	return a;
}
void connect(int a,int b)
{
	int i = root(a);
	int j = root(b);
	if(i == j)
	{
		return ;
	}
	if(sz[i] >= sz[j])
	{
		dsu[j] = i;
		sz[i] += sz[j];
	}
	else
	{
		dsu[i] = j;
		sz[j] += sz[i];
	}
}
main(){
	//freopen("input.txt","rt",stdin);
	int q;
	cin>>q;
	while(q--)
	{
		int n;
		cin>>n;
		int arr[n];
		for(int i=0;i<n;i++)
		{
			cin>>arr[i];
		}
		init(n);
		for(int i=0;i<n;i++)
		{
			connect(i+1,arr[i]);
		}
		for(int i=0;i<n;i++)
		{
			cout<<sz[root(i+1)]<<" ";
		}
		cout<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	long long t;
	cin >> t;
	while(t--)
	{
		long long n,i,c,nu;
		cin >> n;
		long long arr[n+1],dest[n+1];
		for(i=1;i<=n;i++)
			cin >> arr[i];
		map < pair<long long,long long> ,long long > mp;
		pair<long long,long long> p,p1;
		for(i=1;i<=n;i++)
		{
			nu=arr[i];
			c=1;
			p.first=i;
			p.second=nu;
			if(mp[p]!=0)
			{
				p1.first=i;
			    p1.second=nu;
			    mp[p1]=1+mp[p];
			}
			else
			{
			mp[p]=c;
			while(nu!=i)
			{
				c++;
				nu=arr[nu];
				p.first=i;
				p.second=nu;
				if(mp[p]!=0)
			    {
				p1.first=i;
			    p1.second=nu;
			    mp[p1]=c+mp[p];
			    break;
				}
				else
			    mp[p]=c;
			}
			}
		}
		for(i=1;i<=n;i++)
		{
			p.first=i;
			p.second=i;
			cout << mp[p] << " ";
		}
		cout << '\n';
	}

}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	long long t;
	cin >> t;
	while(t--)
	{
		long long n,i,c,p;
		cin >> n;
		long long arr[n+1],dest[n+1];
		for(i=1;i<=n;i++)
			cin >> arr[i];
		for(i=1;i<=n;i++)
		{
			p=arr[i];
			c=1;
			while(p!=i)
			{
				c++;
				p=arr[p];
			}
			dest[i]=c;
		}
		for(i=1;i<=n;i++)
			cout << dest[i] << " ";
		cout << '\n';
	}

}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	long long t;
	cin >> t;
	while(t--)
	{
		long long n,i,c,nu;
		cin >> n;
		long long arr[n+1],dest[n+1];
		for(i=1;i<=n;i++)
			cin >> arr[i];
		map < pair<long long,long long> ,long long > mp;
		pair<long long,long long> p,p1;
		for(i=1;i<=n;i++)
		{
			nu=arr[i];
			c=1;
			p.first=i;
			p.second=nu;
			if(mp[p]!=0)
			{
				p1.first=i;
			    p1.second=nu;
			    mp[p1]=1+mp[p];
			}
			else
			mp[p]=c;
			while(nu!=i)
			{
				c++;
				nu=arr[nu];
				p.first=i;
				p.second=nu;
				if(mp[p]!=0)
			    {
				p1.first=i;
			    p1.second=nu;
			    mp[p1]=c+mp[p];
			    break;
				}
				else
			    mp[p]=c;
			}
		}
		for(i=1;i<=n;i++)
		{
			p.first=i;
			p.second=i;
			cout << mp[p] << " ";
		}
		cout << '\n';
	}

}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
   ll q;ll n;
   cin>>q;
   while(q--)
   { int count=0;  ll temp =0;  bool flag = true;
       cin>>n;
       vector<ll> v(n);
       for(ll i = 0 ; i<n ; i++)
       {
           cin>>v[i];
       }

        for(ll i = 0 ; i<n ; i++)
        {count = 0 ; flag = true;
            temp = v[i];int pos = i;
            v[i]=v[v[i]-1];count++;
            while(v[i]!=pos+1)
         {  v[i]=v[v[i]-1];
             count++; flag = false;
}            v[i]=temp;
if(flag)
            cout<<count<<" ";
            else
            cout<<count+1<<" ";
        }
        cout<<endl;

   }

}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
   ll q;ll n;
   cin>>q;
   while(q--)
   { int count=0;  ll temp =0;  bool flag = true;
       cin>>n;
       vector<ll> v(n);
       for(ll i = 0 ; i<n ; i++)
       {
           cin>>v[i];
       }

        for(ll i = 0 ; i<n ; i++)
        {count = 1 ; //flag = true;
            temp = v[i];int pos = i;
            //v[i]=v[v[i]-1];
            while(v[i]-1!=pos)
         {  v[i]=v[v[i]-1];
             count++;// flag = false;
}            v[i]=temp;
             //if(flag)
            cout<<count<<" ";
           // else
            //cout<<count+1<<" ";
        }
        cout<<endl;

   }

}//1 1 1 1 1
//3 3 3
//2 3 3 2 1 3
//1
//2 2 2 2
//4 4 4 1 4


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
   ll q;ll n;
   cin>>q;
   while(q--)
   { int count=0;  ll temp =0;  bool flag = true;
       cin>>n;
       vector<ll> v(n);
       for(ll i = 0 ; i<n ; i++)
       {
           cin>>v[i];
       }

        for(ll i = 0 ; i<n ; i++)
        {count = 0 ;
            temp = v[i];int pos = i;
            v[i]=v[v[i]-1];count++;
            while(v[i]!=pos+1)
         {  v[i]=v[v[i]-1];
             count++; flag = false;
}            v[i]=temp;
if(flag)
            cout<<count<<" ";
            else
            cout<<count+1<<" ";
        }
        cout<<endl;

   }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector<ll> otc;
vector<ll> v;
vector<ll> otv;
ll a, s, a2, s2;

void fun( ll n, ll k, ll j)
{
    ll i;
    //cout<<n<<" "<<k<<" "<<j<<" test"<<endl;
    if((j==n)&&(k>0)){
        for(i=0;i<a;i++)
        if(otv[i]==1){otc[i]=k;}//cout<<i<<" "<<otc[i]<<" vivod"<<endl;}
    } else {otv[n]=1;n=v[n];k++;fun(v,n,otv,k,j);}

}
int main()
{

    ll  d, kk, i, j;
    cin>>kk;
    for(i=0;i<kk;i++)
    {
    //vector <ll> k(10000,0);
        vector <ll> v;
        vector<ll> otv(10000,0);

       // v.clear();
        cin>>a;
        otc.resize(a+5,0);
        v.resize(a+5,0);
        otv.resize(a+5,0);
        for(j=0;j<a;j++)
        {
            cin>>s;
            v.push_back(s-1);
        }
        for(j=0;j<a;j++)
        {
            if(otc[j]==0){fun(v,j,otv,0, j);}//cout<<j<<" "<<otc[j]<<endl;}
        }
        for(j=0;j<a;j++)
            cout<<otc[j]<<" ";
        cout<<endl;
        otc.clear();
        v.clear();
        otv.clear();

    }

}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
vector<ll> otc;
vector<ll> v;
vector<ll> otv;
ll a, s, a2, s2;

void fun( ll n, ll k, ll j)
{
    ll i;
    //cout<<n<<" "<<k<<" "<<j<<" "<<v[j]<<" test"<<endl;
    if((j==n)&&(k>0)){
        for(i=0;i<a;i++)
        if(otv[i]==1){otc[i]=k;}//cout<<i<<" "<<otc[i]<<" vivod"<<endl;}
    } else {otv[n]=1;n=v[n];k++;fun(n,k,j);}

}
int main()
{

    ll  d, kk, i, j;
    cin>>kk;
    for(i=0;i<kk;i++)
    {
    //vector <ll> k(10000,0);

       // v.clear();
        cin>>a;
        otc.resize(a+5,0);
        //v.resize(a+5,0);
        for(j=0;j<a;j++)
        {
            cin>>s;
            v.push_back(s-1);
        }
        for(j=0;j<a;j++)
        {
        otv.resize(a+5,0);
            if(otc[j]==0){fun(j,0, j);}//cout<<j<<" "<<otc[j]<<endl;}
        otv.clear();
        }
        for(j=0;j<a;j++)
            cout<<otc[j]<<" ";
        cout<<endl;
        otc.clear();
        v.clear();

    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector<ll> otc;
vector<ll> v;
vector<ll> otv;
ll a, s, a2, s2;

void fun( ll n, ll k, ll j)
{
    ll i;
    //cout<<n<<" "<<k<<" "<<j<<" test"<<endl;
    if((j==n)&&(k>0)){
        for(i=0;i<a;i++)
        if(otv[i]==1){otc[i]=k;}//cout<<i<<" "<<otc[i]<<" vivod"<<endl;}
    } else {otv[n]=1;n=v[n];k++;fun(n,k,j);}

}
int main()
{

    ll  d, kk, i, j;
    cin>>kk;
    for(i=0;i<kk;i++)
    {
    //vector <ll> k(10000,0);
        vector <ll> v;
        vector<ll> otv(10000,0);

       // v.clear();
        cin>>a;
        otc.resize(a+5,0);
        v.resize(a+5,0);
        otv.resize(a+5,0);
        for(j=0;j<a;j++)
        {
            cin>>s;
            v.push_back(s-1);
        }
        for(j=0;j<a;j++)
        {
            if(otc[j]==0){fun(j,0, j);}//cout<<j<<" "<<otc[j]<<endl;}
        }
        for(j=0;j<a;j++)
            cout<<otc[j]<<" ";
        cout<<endl;
        otc.clear();
        v.clear();
        otv.clear();

    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector<ll> otc;
ll a, s, a2, s2;

void fun(vector<ll> v, ll n,vector<ll> otv, ll k, ll j)
{
    ll i;
    //cout<<n<<" "<<k<<" "<<j<<" test"<<endl;
    if((j==n)&&(k>0)){
        for(i=0;i<a;i++)
        if(otv[i]==1){otc[i]=k;}//cout<<i<<" "<<otc[i]<<" vivod"<<endl;}
    } else {otv[n]=1;n=v[n];k++;fun(v,n,otv,k,j);}

}
int main()
{

    ll  d, kk, i, j;
    cin>>kk;
    for(i=0;i<kk;i++)
    {
    //vector <ll> k(10000,0);
        vector <ll> v;
        vector<ll> otv(10000,0);

       // v.clear();
        cin>>a;
        otc.resize(a+5,0);
        for(j=0;j<a;j++)
        {
            cin>>s;
            v.push_back(s-1);
        }
        for(j=0;j<a;j++)
        {
            if(otc[j]==0){fun(v,j,otv,0, j);}//cout<<j<<" "<<otc[j]<<endl;}
        }
        for(j=0;j<a;j++)
            cout<<otc[j]<<" ";
        cout<<endl;
        otc.clear();

    }

}


#include<iostream>
using namespace std;

int main()
{
    int q;
    cin >> q;
    while(q--){
        int n;
        cin >> n;
        int P[n];

        for(int i=0; i<n; i++)
            cin >> P[i];

        for(int i=0; i<n; i++){
            int first = P[i], next = P[P[i]-1], days = 1;
            while(next != first){
                days++; next = P[next-1];
            }
            cout << days << " ";
        }
        cout << endl;
    }

    return 0;
}


#include<iostream>
using namespace std;

int main()
{
    int q;
    cin >> q;
    while(q--){
        int n;
        cin >> n;
        int P[n];

        for(int i=0; i<n; i++)
            cin >> P[i];

        for(int i=0; i<n; i++){
            int first = P[i], next = P[P[i]-1]; days = 1;
            while(next != first){
                days++; next = P[next-1];
            }
            cout << days << " ";
        }
        cout << endl;
    }

    return 0;
}


#include<iostream>
using namespace std;

int main()
{
    int q;
    cin >> q;
    while(q--){
        int n;
        cin >> n;
        int P[n];

        for(int i=0; i<n; i++)
            cin >> P[i];

        for(int i=0; i<n; i++){
            int first = P[i], next = P[P[i]-1]; days = 1;
            while(next != first){
                days++; next = P[next];
            }
            cout << days << " ";
        }
        cout << endl;
    }

    return 0;
}


#include<iostream>
using namespace std;

int main()
{
    int q;
    cin >> q;
    while(q--){
        int n;
        cin >> n;
        int P[n];

        for(int i=0; i<n; i++)
            cin >> P[i];

        for(int i=0; i<n; i++){
            int first = P[i], next = P[P[i]-1], days = 1;
            while(next != first){
                days++; next = P[next];
            }
            cout << days << " ";
        }
        cout << endl;
    }

    return 0;
}


    #include<bits/stdc++.h>

    using namespace std;

    int main()
    {
        int t;  cin >> t;
        while(t--)
        {
            int n;  cin >> n;

            int arr[n+1];
            for(int i=1;i<=n;i++)
            {
                cin >> arr[i];
            }

            int ans[n+1]={0};

            for(int i=1;i<=n;i++)
            {
                int stpoint=i, val=i, cnt=0;
                while(cnt != 5)
                {
                    //cout << stpoint << " " << val << " " << cnt << endl;
                    cnt++;
                    val=arr[val];
                    if(val == stpoint)
                    {
                        break;
                    }
                }
                ans[i]=cnt;
            }

            for(int i=1;i<=n;i++)
                cout << ans[i] << " ";
            cout << endl;
        }
        return 0;
    }


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int t;  cin >> t;
    while(t--)
    {
        int n;  cin >> n;
        int arr[n];

        for(int i=0;i<n;i++)
        {
            cin >> arr[i];
        }

        int ans[n];
        for(int i=0;i<n;i++)
        {
            int stpoint=i, val=i, cnt=0;
            while(1)
            {
                cnt++;
                val=arr[i];
                if(val == stpoint)
                {
                    break;
                }
            }
            ans[i]=cnt;
        }

        for(int i=0;i<n;i++)
            cout << ans[i] << " ";
        cout << endl;
    }
    return 0;
}


    #include<bits/stdc++.h>

    using namespace std;

    int main()
    {
        int t;  cin >> t;
        while(t--)
        {
            int n;  cin >> n;

            int arr[n+1];
            for(int i=1;i<=n;i++)
            {
                cin >> arr[i];
            }

            int ans[n+1]={0};

            for(int i=1;i<=n;i++)
            {
                int stpoint=i, val=i, cnt=0;
                while(1)
                {
                    //cout << stpoint << " " << val << " " << cnt << endl;
                    cnt++;
                    val=arr[val];
                    if(val == stpoint)
                    {
                        break;
                    }
                }
                ans[i]=cnt;
            }

            for(int i=1;i<=n;i++)
                cout << ans[i] << " ";
            cout << endl;
        }
        return 0;
    }


#include<bits/stdc++.h>
using namespace std;
int find_parent(int u,int parent[])
{
    int cnt=1;
    if(parent[u]==u)
        return cnt;
    return cnt+=(parent[u]=find_parent(parent[u],parent));
}
int main ()
{
    int q,n,i;
    cin>>q;
    while(q--)
    {
        cin>>n;
        int ans;
        int parent[n+1];
        for(i=1;i<=n;++i)
            cin>>parent[i];
        for(i=1;i<=n;++i)
        {
            ans=find_parent(i,parent);
            cout<<ans<<" ";
        }
        cout<<"\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
int find_parent(int u,int parent[])
{
    int cnt=1;
    if(parent[u]==u)
        return cnt;
    return cnt+=find_parent(parent[u],parent);
}
int main ()
{
    int q,n,i;
    cin>>q;
    while(q--)
    {
        cin>>n;
        int ans;
        int parent[n+1];
        for(i=1;i<=n;++i)
            cin>>parent[i];
        for(i=1;i<=n;++i)
        {
            ans=find_parent(i,parent);
            cout<<ans<<" ";
        }
        cout<<"\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
int find_parent(int u,int parent[])
{
    int cnt=1;
    if(parent[u]==u)
        return cnt;
    return cnt+=find_parent(parent[u],parent);
}
int main ()
{
    int q,n,i;
    cin>>q;
    while(q--)
    {
        cin>>n;
        int ans;
        int parent[n+1];
        for(i=1;i<=n;++i)
            cin>>parent[i];
        for(i=1;i<=n;++i)
        {
            ans=(i,parent);
            cout<<ans<<" ";
        }
        cout<<"\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
int find_parent(int u,vector<int>parent)
{
    int cnt=1;
    if(parent[u]==u)
        return cnt;
    return cnt+=find_parent(parent[u],parent);
}
int main ()
{
    int q,n,i;
    cin>>q;
    while(q--)
    {
        cin>>n;
        int ans;
        vector<int>parent(n+1);
        for(i=1;i<=n;++i)
            cin>>parent[i];
        for(i=1;i<=n;++i)
        {
            ans=(i,parent);
            cout<<ans<<" ";
        }
        cout<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	int q;
	cin>>q;
	for (int i = 0; i < q; ++i)
	{
		int n;
		cin>>n;
		vector<int>p(n);
		for (int j = 0; j < n; ++j)
		{
			cin>>p[j];
			--p[j];
		}
		for (int j = 0; j < n; ++j)
		{
			int cnt = 0;
			int k = j;
			do
			{
				++cnt;
				k = p[k];
			}while (k != j);
			cout<<cnt<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define pb push_back

int dfs(vector<int> v,int i,set<int> visited,int count)
{
    if (visited.find(i)!=visited.end())
       return count;
    else {
      visited.insert(i);
      return dfs(v,v[i],visited,count+1);
    }
}

int main(){
      ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
      int t=0;
      cin>>t;
      while(t--)
      {
        int n=0;
        cin>>n;
        vector<int> v;
        v.pb(0);
        int a=0;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            v.pb(a);
        }
        int arr[n+1]={0};
        for(int i=1;i<=n;i++)
        {
           set<int> visited;
           int count=0;
           arr[i]=dfs(v,i,visited,count);
        }
        for(int i=1;i<=n;i++)
           cout<<arr[i]<<" ";
        cout<<endl;
      }
      return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define pb push_back

int dfs(vector<int> v,int i,set<int> visited,int count)
{
    if (visited.find(i)!=visited.end())
       return count;
    else {
      visited.insert(i);
      return dfs(v,v[i],visited,count+1);
    }
}

int main(){
      int t=0;
      cin>>t;
      while(t--)
      {
        int n=0;
        cin>>n;
        vector<int> v;
        v.pb(0);
        int a=0;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            v.pb(a);
        }
        int arr[n+1]={0};
        for(int i=1;i<=n;i++)
        {
           set<int> visited;
           int count=0;
           arr[i]=dfs(v,i,visited,count);
        }
        for(int i=1;i<=n;i++)
           cout<<arr[i]<<" ";
        cout<<endl;
      }
      return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define pb push_back

int main(){
      int t=0;
      cin>>t;
      while(t--)
      {
        int n=0;
        cin>>n;
        vector<int> v;
        v.pb(0);
        int a=0;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            v.pb(a);
        }
        int arr[n+1]={0};
        for(int i=1;i<=n;i++)
        {
           int pos=v[i],ans=1;
           while(pos!=i)
           {
              pos=v[pos];
              ans++;
           }
           arr[i]=ans;
        }
        for(int i=1;i<=n;i++)
           cout<<arr[i]<<" ";
        cout<<endl;
      }
      return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t, n, arr[269];

    cin>>t;
    for (int i=0; i<t; i++){
        int output[269], pos=0;
        cin>>n;
        for (int j=0; j<n; j++){
            cin>>arr[j];
        }
        for (int j=0; j<n; j++){
           pos=j;
           for (int c=1; c<n; c++){
               int b=j+1;
               b=arr[b-1];
               if (b==pos){
                   output[j]=c;
               }
           }
        }
        for (int j=0; j<n; j++){
            cout<<output[j]<<" ";
        }
        cout<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t, n, arr[269];

    cin>>t;
    for (int i=0; i<t; i++){
        int output[269], pos=0;
        cin>>n;
        for (int j=0; j<n; j++){
            cin>>arr[j];
        }
        for (int j=0; j<n; j++){
           pos=j+1;
           int b=j+1;
           for (int c=1; c<n; c++){
               b=arr[b-1];
               if (b==pos){
                   output[j]=c;
                   break;
               }
           }
        }
        for (int j=0; j<n; j++){
            cout<<output[j]<<" ";
        }
        cout<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t, n, arr[269];

    cin>>t;
    for (int i=0; i<t; i++){
        int output[269], pos=0;
        cin>>n;
        for (int j=0; j<n; j++){
            cin>>arr[j];
        }
        for (int j=0; j<n; j++){
           pos=j;
           int b=j+1;
           for (int c=1; c<n; c++){
               b=arr[b-1];
               if (b==pos){
                   output[j]=c;
               }
           }
        }
        for (int j=0; j<n; j++){
            cout<<output[j]<<" ";
        }
        cout<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t, n, arr[269];

    cin>>t;
    for (int i=0; i<t; i++){
        int output[269], pos=0;
        cin>>n;
        for (int j=0; j<n; j++){
            cin>>arr[j];
        }
        for (int j=0; j<n; j++){
           pos=j+1;
           int b=j+1;
           for (int c=1; c<n+1; c++){
               b=arr[b-1];
               if (b==pos){
                   output[j]=c;
                   break;
               }
           }
        }
        for (int j=0; j<n; j++){
            cout<<output[j]<<" ";
        }
        cout<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<int>adj[205];
int siz=0;
int cc=0;
int t,n;
int a[205];
int vis[205];
int size_dict[205];
void dfs(int u)
{
    vis[u]=cc;
    siz+=1;
    for(int i=0;i<adj[u].size();i++)
    {
        if(!vis[adj[u][i]])
        {
            dfs(adj[u][i]);
        }
    }
}
int main()
{
    cin>>t;
    while(t--)
    {

        cin>>n;
        for(int i=0;i<=204;i++)
        {
            adj[i].clear();
        }
        for(int i=0;i<=204;i++)vis[i]=0;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            adj[i+1].push_back(a[i]);
            adj[a[i]].push_back(i+1);
        }
        for(int i=0;i<n;i++)
        {
            if(!vis[i+1])
            {
                cc++;
                dfs(i+1);
                size_dict[cc]=siz;
                siz=0;
            }
        }
        for(int i=1;i<=n;i++)
        {
            cout<<size_dict[vis[i]]<<" ";
        }
        cout<<"\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
vector<int>adj[205];
int siz=0;
int cc=0;
int t,n;
int a[205];
int vis[205];
int size_dict[205];
void dfs(int u)
{
    vis[u]=cc;
    siz+=1;
    for(int i=0;i<adj[u].size();i++)
    {
        if(!vis[adj[u][i]])
        {
            dfs(adj[u][i]);
        }
    }
}
int main()
{
    cin>>t;
    while(t--)
    {

        cin>>n;
        for(int i=0;i<=n;i++)
        {
            adj[i].clear();
        }
        for(int i=0;i<=n;i++)vis[i]=0;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            adj[i+1].push_back(a[i]);
            adj[a[i]].push_back(i+1);
        }
        for(int i=0;i<n;i++)
        {
            if(!vis[i+1])
            {
                cc++;
                dfs(i+1);
                size_dict[cc]=siz;
                siz=0;
            }
        }
        for(int i=1;i<=n;i++)
        {
            cout<<size_dict[vis[i]]<<" ";
        }
        cout<<"\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
vector<int>adj[205];
int siz=0;
int cc=0;
int t,n;
int a[205];
int vis[205];
int size_dict[205];
void dfs(int u)
{
    vis[u]=cc;
    siz+=1;
    for(int i=0;i<adj[u].size();i++)
    {
        if(!vis[adj[u][i]])
        {
            dfs(adj[u][i]);
        }
    }
}
int main()
{
    cin>>t;
    while(t--)
    {

        cin>>n;
        for(int i=0;i<=204;i++)
        {
            adj[i].clear();
        }
        for(int i=0;i<=204;i++)vis[i]=0;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];

            adj[i+1].push_back(a[i]);
            adj[a[i]].push_back(i+1);
        }
        cc=0;
        for(int i=0;i<n;i++)
        {
            if(!vis[i+1])
            {
                cc++;
                dfs(i+1);
                size_dict[cc]=siz;
                siz=0;
            }
        }
        for(int i=1;i<=n;i++)
        {
            cout<<size_dict[vis[i]]<<" ";
        }
        cout<<"\n";
    }
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll arr[n];
		for(ll i=0;i<n;i++)
		{
			cin>>arr[i];
	    }
	    ll visited[n];
	    memset(visited,0,sizeof(visited));
	    ll ans1[n];
	    memset(ans1,0,sizeof(ans1));
	    for(ll i=0;i<n;i++)
	    {
	    	if(!visited[arr[i]-1])
	    	{
				ll ans=0;
				vector<ll> curr;
				while(!visited[arr[i]-1])
	    		{
	    			ans+=1;
	    			curr.push_back(arr[i]);
	    			visited[arr[i]-1]=true;
	    			i=arr[i]-1;
				}
				for(ll i=0;i<curr.size();i++)
				{
					ans1[curr[i]-1]=ans;
				}
		    }
		}
		for(ll i=0;i<n;i++)
		{
			cout<<ans1[i];
		}
		cout<<endl;
	}
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll arr[n];
		for(ll i=0;i<n;i++)
		{
			cin>>arr[i];
	    }
	    ll visited[n];
	    memset(visited,0,sizeof(visited));
	    ll ans1[n];
	    memset(ans1,0,sizeof(ans1));
	    if(n==1)
	    {
	    	cout<<1<<endl;
	    	continue;
		}
	    for(ll i=0;i<n;i++)
	    {
	    	if(!visited[arr[i]-1])
	    	{
				ll ans=0;
				vector<ll> curr;
				while(!visited[arr[i]-1])
	    		{
	    			ans+=1;
	    			curr.push_back(arr[i]);
	    			visited[arr[i]-1]=true;
	    			i=arr[i]-1;
				}
				for(ll i=0;i<curr.size();i++)
				{
					ans1[curr[i]-1]=ans;
				}
		    }
		}
		for(ll i=0;i<n;i++)
		{
			cout<<ans1[i]<<" ";
		}
		cout<<endl;
	}
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll arr[n];
		for(ll i=0;i<n;i++)
		{
			cin>>arr[i];
	    }
	    ll visited[n];
	    memset(visited,0,sizeof(visited));
	    ll ans1[n];
	    memset(ans1,0,sizeof(ans1));
	    if(n==1)
	    {
	    	cout<<1<<endl;
	    	continue;
		}
	    for(ll i=0;i<n;i++)
	    {
	    	if(!visited[arr[i]-1])
	    	{
				ll ans=0;
				vector<ll> curr;
				while(!visited[arr[i]-1])
	    		{
	    			ans+=1;
	    			curr.push_back(arr[i]);
	    			visited[arr[i]-1]=true;
	    			i=arr[i]-1;
				}
				for(ll i=0;i<curr.size();i++)
				{
					ans1[curr[i]-1]=ans;
				}
		    }
		}
		for(ll i=0;i<n;i++)
		{
			cout<<ans1[i];
		}
		cout<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<pair<int,int>>v;
        for(int i=0;i<n;i++){
            int num;
            cin>>num;
            v.push_back({i+1,num});
        }
        for(int i=0;i<n;i++){
            stack<int>st;
            int pos=i+1;
            st.push(v[i].second);
            int count=0;
            int curr=v[i].second;
            int des=i+1;
            while(curr != des){
            curr=st.top();
            st.pop();
            st.push(v[curr-1].second);
            count++;
            }cout<<max(1,count)<<" ";
        }
        cout<<endl;
    }

}


#include <iostream>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
vector<int> arr[200001];//arr[200001];
bool vis[200001];
int d;
void dfs(int node){
    d++;
    vis[node] = true;
    for(int child : arr[node]){
        if(!vis[child])
            dfs(child);
    }
}

int main() {
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        int brr[n+1];
        for(int i = 0 ; i < 200001 ; i++)     vis[i] = 0;
        for(int i = 0 ; i < 200001 ; i++)     arr[i].clear();
        for(int i = 1 ; i <= n ; i++)    cin >> brr[i];
        int res[n+1] = {0};

        for(int i = 0 ; i < n ; i++){
            arr[brr[i+1]].push_back(brr[brr[i+1]]);
            arr[brr[brr[i+1]]].push_back(brr[i+1]);
        }
        for(int i = 1 ; i <= n ; i++){
            if(!vis[i])
            {
                dfs(i);

                res[i] = d;

                d = 0;
            }
        }
        for(int i = 1 ; i <= n ; i++){
            if(!res[i]) continue;
            //vis[i] = true;
            for(int c : arr[i]){
                    res[c] = res[i];
            }
        }
        for(int i = 1 ; i <= n ; i++)   cout<<res[i]<<" ";
        cout<<"\n";
    }
}


using namespace std;

int main() {

	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		int n;
		cin >> n;
		vector<int> p(n);
		for (int j = 0; j < n; ++j) {
			cin >> p[j];
			--p[j];
		}
		for (int j = 0; j < n; ++j) {
			int cnt = 0;
			int k = j;
			do {
				++cnt;
				k = p[k];
			} while (k != j);
			cout << cnt << " ";
		}
		cout << endl;
	}

	return 0;
}


using namespace std;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		int n;
		cin >> n;
		vector<int> p(n);
		for (int j = 0; j < n; ++j) {
			cin >> p[j];
			--p[j];
		}
		for (int j = 0; j < n; ++j) {
			int cnt = 0;
			int k = j;
			do {
				++cnt;
				k = p[k];
			} while (k != j);
			cout << cnt << " ";
		}
		cout << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		int n;
		cin >> n;
		vector<int> p(n);
		for (int j = 0; j < n; ++j) {
			cin >> p[j];
			--p[j];
		}
		for (int j = 0; j < n; ++j) {
			int cnt = 0;
			int k = j;
			do {
				++cnt;
				k = p[k];
			} while (k != j);
			cout << cnt << " ";
		}
		cout << endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int N = 2e5+5;
int a[N], vis[N];
int Find(int x) {
  return a[x] == x ? x : a[x] = Find(a[x]);
}
void Merge(int x, int y) {
  int fx = Find(x), fy = Find(y);
  if(fx != fy) {
    a[fx] = fy;
  }
}
int main() {
  int t; read(t);
  int n, x;
  while(t--) {
    read(n);
    memset(vis, 0, sizeof vis);
    for(int i = 1; i <= n; ++i) a[i] = i;
    for(int i = 1; i <= n; ++i) {
      read(x); Merge(i, x);
    }
    for(int i = 1; i <= n; ++i) {
      vis[Find(a[i])]++;
    }
    for(int i = 1; i <= n; ++i) {
      printf("%d%c", vis[a[i]], i == n ? '\n' : ' ');
    }
  }
  return 0;
}


#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int N = 2e5+5;
int a[N], vis[N];
int Find(int x) {
  return a[x] == x ? x : a[x] = Find(a[x]);
}
void Merge(int x, int y) {
  int fx = Find(x), fy = Find(y);
  if(fx != fy) {
    a[fx] = fy;
  }
}
int main() {
  int t;
  scanf("%d", &t);
  int n, x;
  while(t--) {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) a[i] = i, vis[i] = 0;
    for(int i = 1; i <= n; ++i) {
      scanf("%d", &x);
      Merge(i, x);
    }
    for(int i = 1; i <= n; ++i) {
      vis[Find(a[i])]++;
    }
    for(int i = 1; i <= n; ++i) {
      printf("%d%c", vis[Find(a[i])], i == n ? '\n' : ' ');
    }
  }
  return 0;
}
/*
12
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
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
*/


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;
const int N = 510;
int Q, n, a[N], cnt, color[N], col;
int head[N], nextt[N], to[N];
int dfn[N], low[N], num, ans[N];
stack<int> st;
bool vis[N];

inline void add(int x, int y) {
	nextt[++cnt] = head[x];
	to[cnt] = y; head[x] = cnt;
}
inline void tarjan(int u) {
	low[u] = dfn[u] = ++num; st.push(u); vis[u] = true;
	for(int i = head[u]; i; i = nextt[i]) {
		int v = to[i];
		if(!dfn[v]) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
		}
		else if(vis[v]) {
			low[u] = min(low[u], dfn[v]);
		}
	}
	if(low[u] == dfn[u]) {
		int val[N], tot = 0; val[++tot] = u;
		color[u] = ++col;
		int p = st.top(); st.pop();
		while(p != u) {
			val[++tot] = p; vis[p] = false;
			color[p] = col; p = st.top(); st.pop();
		}
		for(int i = 1; i <= tot; i++) {
			ans[val[i]] = tot;
		}
	}
}

int main() {
	scanf("%d", &Q);
	while(Q--) {
		while(!st.empty()) st.pop();
		memset(head, 0, sizeof(head));
		memset(nextt, 0, sizeof(nextt));
		memset(to, 0, sizeof(to)); memset(color, 0, sizeof(color));
		memset(ans, 0, sizeof(ans)); memset(dfn, 0, sizeof(dfn));
		memset(low, 0, sizeof(low));
		scanf("%d", &n);
		for(int i = 1; i <= n; i++) scanf("%d", &a[i]), add(i, a[i]);
		for(int i = 1; i <= n; i++) {
			if(!color[i]) tarjan(i);
		}
		for(int i = 1; i <= n; i++)
			printf("%d ", ans[i]);
		printf("\n");
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;
const int N = 1010;
int Q, n, a[N], cnt, color[N], col;
int head[N], nextt[N], to[N];
int dfn[N], low[N], num, ans[N];
stack<int> st;
bool vis[N];

inline void add(int x, int y) {
	nextt[++cnt] = head[x];
	to[cnt] = y; head[x] = cnt;
}
inline void tarjan(int u) {
	low[u] = dfn[u] = ++num; st.push(u); vis[u] = true;
	for(int i = head[u]; i; i = nextt[i]) {
		int v = to[i];
		if(!dfn[v]) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
		}
		else if(vis[v]) {
			low[u] = min(low[u], dfn[v]);
		}
	}
	if(low[u] == dfn[u]) {
		int val[N], tot = 0; val[++tot] = u;
		color[u] = ++col;
		int p = st.top(); st.pop();
		while(p != u) {
			val[++tot] = p; vis[p] = false;
			color[p] = col; p = st.top(); st.pop();
		}
		for(int i = 1; i <= tot; i++) {
			ans[val[i]] = tot;
		}
	}
}

int main() {
	scanf("%d", &Q);
	while(Q--) {
		while(!st.empty()) st.pop();
		memset(head, 0, sizeof(head));
		memset(nextt, 0, sizeof(nextt));
		memset(to, 0, sizeof(to)); memset(color, 0, sizeof(color));
		memset(ans, 0, sizeof(ans)); memset(dfn, 0, sizeof(dfn));
		memset(low, 0, sizeof(low));
		scanf("%d", &n);
		for(int i = 1; i <= n; i++) scanf("%d", &a[i]), add(i, a[i]);
		for(int i = 1; i <= n; i++) {
			if(!color[i]) tarjan(i);
		}
		for(int i = 1; i <= n; i++)
			printf("%d ", ans[i]);
		printf("\n");
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;
const int N = 510;
int Q, n, a[N], cnt, color[N], col;
int head[N], nextt[N], to[N];
int dfn[N], low[N], num, ans[N];
stack<int> st;

inline void add(int x, int y) {
	nextt[++cnt] = head[x];
	to[cnt] = y; head[x] = cnt;
}
inline void tarjan(int u) {
	low[u] = dfn[u] = ++num; st.push(u);
	for(int i = head[u]; i; i = nextt[i]) {
		int v = to[i];
		if(!dfn[v]) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
		}
		else if(!color[v]) {
			low[u] = min(low[u], low[v]);
		}
	}
	if(low[u] == dfn[u]) {
		int val[N], tot = 0; val[++tot] = u;
		color[u] = ++col;
		int p = st.top(); st.pop();
		while(p != u) {
			val[++tot] = p;
			color[p] = col; p = st.top(); st.pop();
		}
		for(int i = 1; i <= tot; i++) {
			ans[val[i]] = tot;
		}
	}
}

int main() {
	scanf("%d", &Q);
	while(Q--) {
		while(!st.empty()) st.pop();
		memset(head, 0, sizeof(head));
		memset(nextt, 0, sizeof(nextt));
		memset(to, 0, sizeof(to)); memset(color, 0, sizeof(color));
		memset(ans, 0, sizeof(ans)); memset(dfn, 0, sizeof(dfn));
		memset(low, 0, sizeof(low));
		scanf("%d", &n);
		for(int i = 1; i <= n; i++) scanf("%d", &a[i]), add(i, a[i]);
		for(int i = 1; i <= n; i++) {
			if(!color[i]) tarjan(i);
		}
		for(int i = 1; i <= n; i++)
			printf("%d ", ans[i]);
		printf("\n");
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;
const int N = 1010;
int Q, n, a[N], cnt, color[N], col;
int head[N], nextt[N], to[N];
int dfn[N], low[N], num, ans[N];
stack<int> st;
bool vis[N];

inline void add(int x, int y) {
	nextt[++cnt] = head[x];
	to[cnt] = y; head[x] = cnt;
}
inline void tarjan(int u) {
	low[u] = dfn[u] = ++num; st.push(u); vis[u] = true;
	for(int i = head[u]; i; i = nextt[i]) {
		int v = to[i];
		if(!dfn[v]) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
		}
		else if(vis[v]) {
			low[u] = min(low[u], dfn[v]);
		}
	}
	if(low[u] == dfn[u]) {
		int val[N], tot = 0; val[++tot] = u;
		color[u] = ++col;
		int p = st.top(); st.pop();
		while(p != u) {
			val[++tot] = p; vis[p] = false;
			color[p] = col; p = st.top(); st.pop();
		}
		for(int i = 1; i <= tot; i++) {
			ans[val[i]] = tot;
		}
	}
}

int main() {
	scanf("%d", &Q);
	while(Q--) {
		while(!st.empty()) st.pop();
		memset(head, 0, sizeof(head));
		memset(nextt, 0, sizeof(nextt));
		memset(to, 0, sizeof(to)); memset(color, 0, sizeof(color));
		memset(ans, 0, sizeof(ans)); memset(dfn, 0, sizeof(dfn));
		memset(low, 0, sizeof(low)); cnt = 0;
		scanf("%d", &n);
		for(int i = 1; i <= n; i++) scanf("%d", &a[i]), add(i, a[i]);
		for(int i = 1; i <= n; i++) {
			if(!color[i]) tarjan(i);
		}
		for(int i = 1; i <= n; i++)
			printf("%d ", ans[i]);
		printf("\n");
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;
const int N = 510;
int Q, n, a[N], cnt, color[N], col;
int head[N], nextt[N], to[N];
int dfn[N], low[N], num, ans[N];
stack<int> st;

inline void add(int x, int y) {
	nextt[++cnt] = head[x];
	to[cnt] = y; head[x] = cnt;
}
inline void tarjan(int u) {
	low[u] = dfn[u] = ++num; st.push(u);
	for(int i = head[u]; i; i = nextt[i]) {
		int v = to[i];
		if(!dfn[v]) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
		}
		else if(!color[v]) {
			low[u] = min(low[u], low[v]);
		}
	}
	if(low[u] == dfn[u]) {
		int val[N], tot = 0; val[++tot] = u;
		color[u] = ++col;
		int p = st.top(); st.pop();
		while(p != u) {
			val[++tot] = p;
			color[p] = col; p = st.top(); st.pop();
		}
		for(int i = 1; i <= tot; i++) {
			ans[val[i]] = tot;
		}
	}
}

int main() {
	scanf("%d", &Q);
	while(Q--) {
		while(!st.empty()) st.pop();
		memset(head, 0, sizeof(head));
		memset(nextt, 0, sizeof(nextt));
		memset(to, 0, sizeof(to)); memset(color, 0, sizeof(color));
		memset(ans, 0, sizeof(ans)); memset(dfn, 0, sizeof(dfn));
		memset(low, 0, sizeof(low));
		scanf("%d", &n);
		for(int i = 1; i <= n; i++) scanf("%d", &a[i]), add(i, a[i]);
		for(int i = 1; i <= n; i++) {
			if(!color[i]) tarjan(i);
		}
		for(int i = 1; i <= n; i++)
			printf("%d ", ans[i]);
		printf("\n");
	}
}

/*

qwq
*/


#include <bits/stdc++.h>
using namespace std;
void fa(long long a[], int sz) {for (int i=0; i<sz; i++) cin >> a[i];}
#define bp cout<<"\nBreakpoint at "<<__LINE__<<'\n';
#define db(x) cout<<'\n'<<"The value of "<<(#x)<<" is "<<x<<'\n';

const int maxN = 207;
int uf[maxN], sz[maxN];

int look(int x)
{
    if(uf[x] == x) {return x;}
    uf[x] = look(uf[x]);
    return uf[x];
}

void join(int a, int b)
{
    if (look(a) == look(b)) return;
    int pa = look(a);
    int pb = look(b);
    sz[pb] += sz[pa];
    uf[pa] = pb;
}

int main()
{
    int q, n;
    cin >> q;

    while (q--)
    {
        cin >> n;
        int p[n];

        for (int i=0; i<n; i++)
        {
            cin >> p[i]; --p[i];
            uf[i] = i; sz[i] = 1;
        }

        for (int i=0; i<n; i++)
            join(i, p[i]);

        for (int i=0; i<n; i++)
            cout << sz[look(i)] << ' ';
        cout << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
void fa(long long a[], int sz) {for (int i=0; i<sz; i++) cin >> a[i];}
#define bp cout<<"\nBreakpoint at "<<__LINE__<<'\n';
#define db(x) cout<<'\n'<<"The value of "<<(#x)<<" is "<<x<<'\n';

const int maxN = 207;
int uf[maxN], sz[maxN];

int look(int x)
{
    if(uf[x] == x) {return x;}
    uf[x] = look(uf[x]);
    return uf[x];
}

void join(int a, int b)
{
    if (look(a) == look(b)) return;
    int pa = look(a);
    int pb = look(b);
    uf[pa] = pb;
    sz[pb] += sz[pa];
}

int main()
{
    int q, n;
    cin >> q;

    while (q--)
    {
        cin >> n;
        int p[n];

        for (int i=0; i<n; i++)
        {
            cin >> p[i]; --p[i];
            uf[i] = i; sz[i] = 1;
        }

        for (int i=0; i<n; i++)
            join(i, p[i]);

        for (int i=0; i<n; i++)
            cout << sz[uf[i]] << ' ';
        cout << '\n';
    }
}


#include<iostream>
#include<vector>
#define ll long long int
using namespace std;

ll findParent(ll i,ll parent[])
{
    if(parent[i]==i)
    return(i);
    else
    return(findParent(parent[i],parent));
}

void union1(ll parent[],ll rank[],ll ans[],ll a,ll b)
{
    ll pa=findParent(a,parent);
    ll pb=findParent(b,parent);
    if(pa==pb)
    return;
    if(rank[pa]<rank[pb]){
    parent[pa]=pb;
    ans[pb]++;}
    else
    {
        if(rank[pa]>rank[pb]){
        parent[pb]=pa;
        ans[pa]++;}
        else
        {
            parent[pb]=pa;
            rank[pa]=rank[pa]+1;
            ans[pa]++;
        }
    }
}

int main()
{
    ll q;
    cin>>q;
    while(q--)
    {
        ll n;
        cin>>n;
        ll arr[n+1];
        for(ll i=1;i<=n;i++)
        {
            cin>>arr[i];
        }
        ll parent[n+1];
        ll rank[n+1];
        ll ans[n+1];
        for(ll i=0;i<=n;i++){
        parent[i]=i;
        rank[i]=1;
        ans[i]=1;
        }
        //now we trying to do union operation for connected componant...
        for(ll i=1;i<=n;i++)
        {
            union1(parent,rank,ans,i,arr[i]);
        }
        for(ll i=1;i<=n;i++)
        {
            ll p=findParent(i,parent);
            cout<<ans[p]<<" ";
        }
        cout<<endl;
    }
    return(0);
}


#include<iostream>
#include<vector>
#define ll long long int
using namespace std;

ll findParent(ll i,ll parent[])
{
    if(parent[i]==i)
    return(i);
    else
    {
          parent[i]=findParent(parent[i],parent);
          return(parent[i]);
    }
}

void union1(ll parent[],ll rank[],ll ans[],ll a,ll b)
{
    ll pa=findParent(a,parent);
    ll pb=findParent(b,parent);
    if(pa==pb)
    return;
    if(rank[pa]<rank[pb]){
    parent[pa]=pb;
    ans[pb]=ans[pb]+ans[pa];}
    else
    {
        if(rank[pa]>rank[pb]){
        parent[pb]=pa;
        ans[pa]=ans[pa]+ans[pb];}
        else
        {
            parent[pb]=pa;
            rank[pa]=rank[pa]+1;
            ans[pa]=ans[pa]+ans[pb];
        }
    }
}

int main()
{
    ll q;
    cin>>q;
    while(q--)
    {
        ll n;
        cin>>n;
        ll arr[n+1];
        for(ll i=1;i<=n;i++)
        {
            cin>>arr[i];
        }
        ll parent[n+1];
        ll rank[n+1];
        ll ans[n+1];
        for(ll i=0;i<=n;i++){
        parent[i]=i;
        rank[i]=1;
        ans[i]=1;
        }
        //now we trying to do union operation for connected componant...
        for(ll i=1;i<=n;i++)
        {
            union1(parent,rank,ans,i,arr[i]);
        }
        for(ll i=1;i<=n;i++)
        {
            ll p=findParent(i,parent);
            cout<<ans[p]<<" ";
        }
        cout<<endl;
    }
    return(0);
}


#include<iostream>
#include<vector>
#define ll long long int
using namespace std;

ll findParent(ll i,ll parent[])
{
    if(parent[i]==i)
    return(i);
    else
    return(findParent(parent[i],parent));
}

void union1(ll parent[],ll rank[],ll ans[],ll a,ll b)
{
    ll pa=findParent(a,parent);
    ll pb=findParent(b,parent);
    if(pa==pb)
    return;
    if(rank[pa]<rank[pb]){
    parent[pa]=pb;
    ans[pb]=ans[pb]+ans[pa];}
    else
    {
        if(rank[pa]>rank[pb]){
        parent[pb]=pa;
        ans[pa]=ans[pa]+ans[pb];}
        else
        {
            parent[pb]=pa;
            rank[pa]=rank[pa]+1;
            ans[pa]=ans[pa]+ans[pb];
        }
    }
}

int main()
{
    ll q;
    cin>>q;
    while(q--)
    {
        ll n;
        cin>>n;
        ll arr[n+1];
        for(ll i=1;i<=n;i++)
        {
            cin>>arr[i];
        }
        ll parent[n+1];
        ll rank[n+1];
        ll ans[n+1];
        for(ll i=0;i<=n;i++){
        parent[i]=i;
        rank[i]=1;
        ans[i]=1;
        }
        //now we trying to do union operation for connected componant...
        for(ll i=1;i<=n;i++)
        {
            union1(parent,rank,ans,i,arr[i]);
        }
        for(ll i=1;i<=n;i++)
        {
            ll p=findParent(i,parent);
            cout<<ans[p]<<" ";
        }
        cout<<endl;
    }
    return(0);
}


#include<bits/stdc++.h>
using namespace std;
#define in(x)  cin>>x
#define o(x)  cout<<x
#define sp cout<<' '
#define nw cout<<endl
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define frm(i,a,n) for(int i=n;i>=a;i--)
#define vc vector<int>
typedef long long int ll;
typedef string st;
ll i,j,k,l,m,n,c,p,q;
ll a[100002],b[100002],x[100002];
int main()
{
  in(q);
  while(q--)
  {
      in(n);
      fr(i,0,n-1)in(a[i]);
      fr(i,0,n-1)
      {  c=1;
          l=a[i];
          while(i!=l-1)l=a[l-1],c++;
          o(c);sp;
      }nw;
  }
}


#include<bits/stdc++.h>
using namespace std;
#define in(x)  cin>>x
#define o(x)  cout<<x
#define sp cout<<' '
#define nw cout<<endl
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define frm(i,a,n) for(int i=n;i>=a;i--)
#define vc vector<int>
typedef long long int ll;
typedef string st;
ll i,j,k,l,m,n,c,p,q;
ll a[100002],b[100002],x[100002];
int main()
{
  st s[500];
  in(n);
  fr(i,0,n-1)in(s[i]);
  fr(i,0,n-1)
  {
      fr(j,0,n-1)
      { c=0;
          if(s[i][j+1]=='o')c++;
          if(s[i][j-1]=='o')c++;
          if(s[i+1][j]=='o')c++;
          if(s[i-1][j]=='o')c++;
          if(c%2){o("NO");return 0;}
      }
  }
  o("YES");
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FAST                     \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
int a[110];
int main()
{
    FAST;
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n; ++i)
        {
            cin >> a[i];
        }
        for (int i = 1; i <= n; ++i)
        {
            int t = 1, pre = i;
            while (a[pre] != i)
            {
                t++;
                pre = a[pre];
            }
            cout << t << " ";
        }
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FAST                     \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
int a[210];
int main()
{
    FAST;
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n; ++i)
        {
            cin >> a[i];
        }
        for (int i = 1; i <= n; ++i)
        {
            int t = 1, pre = i;
            while (a[pre] != i)
            {
                t++;
                pre = a[pre];
            }
            cout << t << " ";
        }
        cout << endl;
    }
    return 0;
}


#include <iostream>

using namespace std;

int v[250][250];

void query(int n,int j)
{
    int i,x,ct;

    for(i=1;i<=n;i++)
    {
        ct=1;

        x=v[j][i];

        while(x!=i)
        {
            ct++;
            x=v[j][x];
        }

        cout<<ct<<" ";
    }

    cout<<"\n";
}

int main()
{
    int q,i,j,n;

    cin>>q;

    for(i=1;i<=q;i++)
    {
        cin>>n;

        v[i][0]=n;

        for(j=1;j<=n;j++)
            cin>>v[i][j];

    }

    for(i=1;i<=q;i++)
    {
         query(v[i][0],i);
    }

    return 0;
}


#include <iostream>

using namespace std;

int v[250];

void query(int n)
{
    int i,x,ct;

    for(i=1;i<=n;i++)
    {
        ct=1;

        x=v[i];

        while(x!=i)
        {
            ct++;
            x=v[x];
        }

        cout<<ct<<" ";
    }

    cout<<"\n";
}

int main()
{
    int q,i,j,n;

    cin>>q;

    for(i=1;i<=q;i++)
    {
        cin>>n;

        for(j=1;j<=n;j++)
            cin>>v[j];

        query(n);
    }

    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define N 100100
using namespace std;

inline char nc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}

inline void read(int &x){
	char c=nc(); int b=1;
	for(;(c<'0' || c>'9');c=nc()) if(c=='-') b=-1;
	for(x=0;(c>='0' && c<='9');x=(x<<3)+(x<<1)+c-'0',c=nc());
	x*=b;
}

struct asd{
	int to,next;
}edge[N*2];

int cnt,head[N];
inline void ADD(int u,int v){
	edge[++cnt].to=v; edge[cnt].next=head[u];
	head[u]=cnt;
}

int a[N],deep[N],t[4];
inline void dfs(int x){
	printf("%d ",x);
	if(x!=1)
		a[x]=t[a[x]+2];
	for(int i=head[x];i;i=edge[i].next)
		if(!deep[edge[i].to]){
			deep[edge[i].to]=deep[x]+1;
			dfs(edge[i].to);
			printf("%d ",x);
			a[x]=t[a[x]+2];
			if(a[edge[i].to]!=1) printf("%d %d ",edge[i].to,x),a[edge[i].to]=1,a[x]=t[a[x]+2];
		}
}

int n,u,v;
int main(){
	read(n);
	t[1]=1; t[3]=-1;
	for(int i=1;i<=n;i++)
		read(a[i]);
	for(int i=1;i<n;i++){
		read(u); read(v);
		ADD(u,v);
		ADD(v,u);
	}
	deep[1]=1;
	dfs(1);
	if(a[1]==-1){
		printf("%d %d %d",edge[head[1]].to,1,edge[head[1]].to);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define N 100100
using namespace std;

inline char nc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}

inline void read(int &x){
	char c=nc(); int b=1;
	for(;(c<'0' || c>'9');c=nc()) if(c=='-') b=-1;
	for(x=0;(c>='0' && c<='9');x=(x<<3)+(x<<1)+c-'0',c=nc());
	x*=b;
}

struct asd{
	int to,next;
}edge[N*2];

int cnt,head[N];
inline void ADD(int u,int v){
	edge[++cnt].to=v; edge[cnt].next=head[u];
	head[u]=cnt;
}

int a[N],deep[N],t[4];
inline void dfs(int x){
	printf("%d ",x);
	if(x!=1)
		a[x]=t[a[x]+2];
	for(int i=head[x];i;i=edge[i].next)
		if(!deep[edge[i].to]){
			deep[edge[i].to]=deep[x]+1;
			dfs(edge[i].to);
			printf("%d ",x);
			a[x]=t[a[x]+2];
			if(a[edge[i].to]!=1) printf("%d %d ",edge[i].to,x),a[edge[i].to]=1;
		}
}

int n,u,v;
int main(){
	read(n);
	t[1]=1; t[3]=-1;
	for(int i=1;i<=n;i++)
		read(a[i]);
	for(int i=1;i<n;i++){
		read(u); read(v);
		ADD(u,v);
		ADD(v,u);
	}
	if(a[1]==-1){
		a[1]=1;
		a[edge[head[1]].to]=t[a[edge[head[1]].to]+2];
		printf("%d ",edge[head[1]].to);
	}
	deep[1]=1;
	dfs(1);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define next next2
using namespace std;
const int MAXN=400010;
int n,w[MAXN];
int head[MAXN],to[MAXN],next[MAXN],cnt=1;
int ans[10000010],tot;
inline void adde(int f,int t)
{
    cnt++,to[cnt]=t,next[cnt]=head[f],head[f]=cnt;
    cnt++,to[cnt]=f,next[cnt]=head[t],head[t]=cnt;
}
void DFS(int x,int fa)
{
    int ch=0;
    ans[++tot]=x;
    w[x]=-w[x];
    for(int i=head[x];i;i=next[i])
        if(to[i]!=fa)
        {
            ch++;
            DFS(to[i],x);
            if(w[to[i]]==-1)
            {
                ans[++tot]=to[i];
                ans[++tot]=x;
                w[to[i]]=-w[to[i]];
                w[x]=-w[x];
            }
        }
    ans[++tot]=fa;
    w[fa]=-w[fa];
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&w[i]);
    int o=0;
    for(int i=1;i<=n;i++) if(w[i]==1) o++;
    if(o==n) {puts("1");return 0;}
    for(int i=1,x,y;i<n;i++) scanf("%d%d",&x,&y),adde(x,y);
    DFS(1,0);
    tot--;
    if(w[1]==1) tot--;
    for(int i=1;i<=tot;i++) printf("%d ",ans[i]);
    return 0;
}


#include <iostream>
#include <cstring>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <deque>
#include <bitset>
#include <algorithm>

using namespace std;

const double eps=1e-10;
const double pi=3.1415926535897932384626433832795;
const double eln=2.718281828459045235360287471352;

#define LL long long
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
#define scan(x) scanf("%d", &x)
#define scan2(x, y) scanf("%d%d", &x, &y)
#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define sqr(x) (x) * (x)
#define pr(x) printf("Case %d: ",x)
#define prn(x) printf("Case %d:\n",x)
#define prr(x) printf("Case #%d: ",x)
#define prrn(x) printf("Case #%d:\n",x)
#define lowbit(x) (x&(-x))

const int maxn=200005;

vector<int> g[maxn];
int fat[maxn];
vector<int> son[maxn];
int now[maxn];
bool has[maxn];
int i,j,k,n;
queue<int> q;

void DFS(int x,int y)
{
    fat[x]=y;
    if(now[x]<0)has[x]=true;
    for(unsigned int i=0;i<g[x].size();i++)
    {
        int v=g[x][i];
        if(fat[v]!=0)continue;
        son[x].push_back(v);
        DFS(v,x);
        if(now[v]<0 || has[v])has[x]=true;
    }
}

void find(int x)
{
    q.push(x);
    if(x>1)now[x]=-now[x];
    if(son[x].size()==0){has[x]=false;return;}
    for(unsigned int i=0;i<son[x].size();i++)
    {
        int v=son[x][i];
        if(has[v]){find(v);now[x]=-now[x];q.push(x);}
    }
    for(unsigned int i=0;i<son[x].size();i++)
    {
        int v=son[x][i];
        if(has[v]){find(v);now[x]=-now[x];q.push(x);}
    }
    if(now[x]<0)has[x]=true;else has[x]=false;
    if(x==1 && has[x])
    {
        q.push(son[1][0]);
        q.push(x);
        q.push(son[1][0]);
    }
}

int main()
{
    scanf("%d",&n);
    memset(has,0,sizeof(has));
    for(i=1;i<=n;i++)scanf("%d",&now[i]);
    for(i=1;i<=n-1;i++)
    {
        scan2(j,k);
        g[j].push_back(k);
        g[k].push_back(j);
    }
    DFS(1,-1);
    while(!q.empty())q.pop();
    find(1);n=0;
    while(!q.empty())
    {
        n++;
        if(n>1)printf(" ");
        printf("%d",q.front());
        q.pop();
    }
    return 0;
}