#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

vector< vector<int> > adj(100007);
bool visited[100007];
bool visited2[100007];
int q, n, count1, ans[100007], tmp;
int p[100007];
int dfs(int x){
    count1 = 1;
    visited[x] = true;
    for(int i = 0; i < adj[x].size(); i++){
        if(!visited[adj[x][i]]){
            dfs(adj[x][i]);
            count1++;
        }

    }
    return count1;

}

void dfs2(int x){
    visited2[x] = true;
    for(int i = 0; i < adj[x].size(); i++){
        if(!visited2[adj[x][i]]){
            ans[adj[x][i]] = tmp;
            dfs2(adj[x][i]);
        }
    }
}

int main(){
    cin >> q;
    while(q--){
        cin >> n;
        for(int i = 0; i < 100007; i++){
            visited[i] = false;
            visited2[i] = false;
        }
        for (size_t y = 1; y <= adj.size(); y++)
        {
            for (size_t x = 1; x <= adj[y].size(); x++)
            {
                adj[y][x] = 0;
            }
        }
        for(int i = 1; i <= n; i++){
            cin >> p[i];
            adj[i].pb(p[i]);
        }
        if(n == 1){
            cout << 1 << endl;
            continue;
        }
        for(int i = 1; i <= n; i++){
            if(!visited[i]){
                ans[i] = dfs(i);
                tmp = ans[i];
                dfs2(i);
            }
            cout << ans[i] << " ";
        }
    }
}


#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

vector< vector<int> > adj(100007);
bool visited[100007];
bool visited2[100007];
int q, n, count1, ans[100007], tmp;
int p[100007];
int dfs(int x){
    count1 = 1;
    visited[x] = true;
    for(int i = 0; i < adj[x].size(); i++){
        if(!visited[adj[x][i]]){
            dfs(adj[x][i]);
            count1++;
        }

    }
    return count1;

}

void dfs2(int x){
    visited2[x] = true;
    for(int i = 0; i < adj[x].size(); i++){
        if(!visited2[adj[x][i]]){
            ans[adj[x][i]] = tmp;
            dfs2(adj[x][i]);
        }
    }
}

int main(){
    cin >> q;
    while(q--){
        cin >> n;
        for(int i = 0; i < 100007; i++){
            visited[i] = false;
            visited2[i] = false;
        }
        MS0(ans);
        for(int i = 1; i <= 100007; i++){
            adj[i].clear();
        }

        for(int i = 1; i <= n; i++){
            cin >> p[i];
            adj[i].pb(p[i]);
        }
        if(n == 1){
            cout << 1 << endl;
            continue;
        }
        for(int i = 1; i <= n; i++){
            if(!visited[i]){
                ans[i] = dfs(i);
                tmp = ans[i];
                dfs2(i);
            }
            cout << ans[i] << " ";
        }
    }
}


#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

vector< vector<int> > adj(100007);
bool visited[100007];
bool visited2[100007];
int q, n, count1, ans[100007], tmp;
int p[100007];
int dfs(int x){
    count1 = 1;
    visited[x] = true;
    for(int i = 0; i < adj[x].size(); i++){
        if(!visited[adj[x][i]]){
            dfs(adj[x][i]);
            count1++;
        }

    }
    return count1;

}

void dfs2(int x){
    visited2[x] = true;
    for(int i = 0; i < adj[x].size(); i++){
        if(!visited2[adj[x][i]]){
            ans[adj[x][i]] = tmp;
            dfs2(adj[x][i]);
        }
    }
}

int main(){
    cin >> q;
    while(q--){
        cin >> n;
        for(int i = 0; i < 100007; i++){
            visited[i] = false;
            visited2[i] = false;
        }
        for (size_t y = 0; y < adj.size(); y++)
        {
            for (size_t x = 0; x < adj[y].size(); x++)
            {
                adj[y][x] = 0;
            }
        }
        for(int i = 1; i <= n; i++){
            cin >> p[i];
            adj[i].pb(p[i]);
        }
        if(n == 1){
            cout << 1 << endl;
            continue;
        }
        for(int i = 1; i <= n; i++){
            if(!visited[i]){
                ans[i] = dfs(i);
                tmp = ans[i];
                dfs2(i);
            }
            cout << ans[i] << " ";
        }
    }
}


#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

vector< vector<int> > adj(1000007);
bool visited[100007];
bool visited2[100007];
int q, n, count1, ans[100007], tmp;
int p[100007];
int dfs(int x){
    count1 = 1;
    visited[x] = true;
    for(int i = 0; i < adj[x].size(); i++){
        if(!visited[adj[x][i]]){
            dfs(adj[x][i]);
            count1++;
        }

    }
    return count1;

}

void dfs2(int x){
    visited2[x] = true;
    for(int i = 0; i < adj[x].size(); i++){
        if(!visited2[adj[x][i]]){
            ans[adj[x][i]] = tmp;
            dfs2(adj[x][i]);
        }
    }
}

int main(){
    cin >> q;
    while(q--){
        cin >> n;
        for(int i = 0; i < 100007; i++){
            visited[i] = false;
            visited2[i] = false;
        }
        MS0(ans);
        for(int i = 1; i <= 100007; i++){
            adj[i].clear();
        }

        for(int i = 1; i <= n; i++){
            cin >> p[i];
            adj[i].pb(p[i]);
        }
        if(n == 1){
            cout << 1 << endl;
            continue;
        }
        for(int i = 1; i <= n; i++){
            if(!visited[i]){
                ans[i] = dfs(i);
                tmp = ans[i];
                dfs2(i);
            }
            cout << ans[i] << " ";
        }
    }
}


#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

vector< vector<int> > adj(100007);
bool visited[100007];
bool visited2[100007];
int q, n, count1, ans[100007], tmp;
int p[100007];
int dfs(int x){
    count1 = 1;
    visited[x] = true;
    for(int i = 0; i < adj[x].size(); i++){
        if(!visited[adj[x][i]]){
            dfs(adj[x][i]);
            count1++;
        }

    }
    return count1;

}

void dfs2(int x){
    visited2[x] = true;
    for(int i = 0; i < adj[x].size(); i++){
        if(!visited2[adj[x][i]]){
            ans[adj[x][i]] = tmp;
            dfs2(adj[x][i]);
        }
    }
}

int main(){
    cin >> q;
    while(q--){
        cin >> n;
        for(int i = 0; i < 100007; i++){
            visited[i] = false;
            visited2[i] = false;
        }
        for (size_t y = 1; y <= adj.size(); y++)
        {
            for (size_t x = 0; x < adj[y].size(); x++)
            {
                adj[y][x] = 0;
            }
        }
        for(int i = 1; i <= n; i++){
            cin >> p[i];
            adj[i].pb(p[i]);
        }
        if(n == 1){
            cout << 1 << endl;
            continue;
        }
        for(int i = 1; i <= n; i++){
            if(!visited[i]){
                ans[i] = dfs(i);
                tmp = ans[i];
                dfs2(i);
            }
            cout << ans[i] << " ";
        }
    }
}


#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

vector< vector<int> > adj(100007);
bool visited[100007];
bool visited2[100007];
int q, n, count1, ans[100007], tmp;
int p[100007];
int dfs(int x){
    count1 = 1;
    visited[x] = true;
    for(int i = 0; i < adj[x].size(); i++){
        if(!visited[adj[x][i]]){
            dfs(adj[x][i]);
            count1++;
        }

    }
    return count1;

}

void dfs2(int x){
    visited2[x] = true;
    for(int i = 0; i < adj[x].size(); i++){
        if(!visited2[adj[x][i]]){
            ans[adj[x][i]] = tmp;
            dfs2(adj[x][i]);
        }
    }
}

int main(){
    cin >> q;
    while(q--){
        cin >> n;
        for(int i = 0; i < 100007; i++){
            visited[i] = false;
            visited2[i] = false;
        }
        MS0(ans);
        for(int i = 1; i <= n; i++){
            adj[i].clear();
        }

        for(int i = 1; i <= n; i++){
            cin >> p[i];
            adj[i].pb(p[i]);
        }
        if(n == 1){
            cout << 1 << endl;
            continue;
        }
        for(int i = 1; i <= n; i++){
            if(!visited[i]){
                ans[i] = dfs(i);
                tmp = ans[i];
                dfs2(i);
            }
            cout << ans[i] << " ";
        }
    }
}


#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

vector< vector<int> > adj(200007);
bool visited[200007];
bool visited2[200007];
int q, n, count1, ans[200007], tmp;
int p[100007];
int dfs(int x){
    count1 = 1;
    visited[x] = true;
    for(int i = 0; i < adj[x].size(); i++){
        if(!visited[adj[x][i]]){
            dfs(adj[x][i]);
            count1++;
        }

    }
    return count1;

}

void dfs2(int x){
    visited2[x] = true;
    for(int i = 0; i < adj[x].size(); i++){
        if(!visited2[adj[x][i]]){
            ans[adj[x][i]] = tmp;
            dfs2(adj[x][i]);
        }
    }
}

int main(){
    cin >> q;
    while(q--){
        cin >> n;
        for(int i = 0; i < 200007; i++){
            visited[i] = false;
            visited2[i] = false;
        }
        MS0(ans);
        for(int i = 1; i <= 200007; i++){
            adj[i].clear();
        }

        for(int i = 1; i <= n; i++){
            cin >> p[i];
            adj[i].pb(p[i]);
        }
        if(n == 1){
            cout << 1 << endl;
            continue;
        }
        for(int i = 1; i <= n; i++){
            if(!visited[i]){
                ans[i] = dfs(i);
                tmp = ans[i];
                dfs2(i);
            }
            cout << ans[i] << " ";
        }
    }
}


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

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int q;
    cin >> q;

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

    	int p[n + 1];

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

    	int ans[n + 1];
    	bool check[n + 1];
    	for (int i = 1; i <= n; i++) {
    		check[n + 1] = false;
    	}
    	int temp;
    	int cur;

    	for (int i = 1; i <= n; i++) {
    		if (!check[i]) {
	    		vector<int> now;
	    		temp = 1;
	    		now.push_back(i);
	    		cur = p[i];
	    		while (cur != i) {
	    			check[cur] = true;
	    			now.push_back(cur);
	    			temp++;
	    			cur = p[cur];
	    		}
	    		for (auto num : now) {
	    			ans[num] = temp;
	    		}
	    	}
    	}

    	for (int i = 1; i <= n; i++) {
    		cout << ans[i] << " ";
    	}
    	cout << "\n";
    }
}


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

static int ans[201];
int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int q;
    cin >> q;

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

    	int p[n + 1];

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

    	bool check[n + 1];
    	for (int i = 1; i <= n; i++) {
    		check[n + 1] = false;
    	}
    	int temp;
    	int cur;

    	for (int i = 1; i <= n; i++) {
    		if (!check[i]) {
	    		vector<int> now;
	    		temp = 1;
	    		now.push_back(i);
	    		cur = p[i];
	    		while (cur != i) {
	    			check[cur] = true;
	    			now.push_back(cur);
	    			temp++;
	    			cur = p[cur];
	    		}
	    		for (auto num : now) {
	    			ans[num] = temp;
	    		}
	    	}
    	}

    	for (int i = 1; i <= n; i++) {
    		cout << ans[i] << " ";
    	}
    	cout << "\n";
    }
}


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

int main() { // debug different answer online
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int q;
    cin >> q;

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

    	int p[n + 1];
    	int ans[n + 1];

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

    	bool check[n + 1];
    	for (int i = 1; i <= n; i++) {
    		check[i] = false;
    	}
    	int temp;
    	int cur;

    	for (int i = 1; i <= n; i++) {
    		if (!check[i]) {
	    		vector<int> now;
	    		temp = 1;
	    		now.push_back(i);
	    		cur = p[i];
	    		while (cur != i) {
	    			check[cur] = true;
	    			now.push_back(cur);
	    			temp++;
	    			cur = p[cur];
	    		}
	    		for (auto num : now) {
	    			ans[num] = temp;
	    		}
	    	}
    	}

    	for (int i = 1; i <= n; i++) {
    		cout << ans[i] << " ";
    	}
    	cout << "\n";
    }
}


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

int main() { // debug different answer online
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int q;
    cin >> q;

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

    	int p[n + 1];
    	int ans[n + 1];

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

    	bool check[n + 1];
    	for (int i = 1; i <= n; i++) {
    		check[n + 1] = false;
    	}
    	int temp;
    	int cur;

    	for (int i = 1; i <= n; i++) {
    		if (!check[i]) {
	    		vector<int> now;
	    		temp = 1;
	    		now.push_back(i);
	    		cur = p[i];
	    		while (cur != i) {
	    			check[cur] = true;
	    			now.push_back(cur);
	    			temp++;
	    			cur = p[cur];
	    		}
	    		for (auto num : now) {
	    			ans[num] = temp;
	    		}
	    	}
    	}

    	for (int i = 1; i <= n; i++) {
    		cout << ans[i] << " ";
    	}
    	cout << "\n";
    }
}


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

int main() { // debug different answer online
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int q;
    cin >> q;

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

    	int p[n + 1];
    	int ans[n + 1];

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

    	bool check[n + 1];
    	for (int i = 1; i <= n; i++) {
    		check[n + 1] = false;
    	}
    	int temp;
    	int cur;

    	for (int i = 1; i <= n; i++) {
    		if (!check[i]) {
	    		vector<int> now;
	    		temp = 1;
	    		now.push_back(i);
	    		cur = p[i];
	    		while (cur != i) {
	    			check[cur] = true;
	    			now.push_back(cur);
	    			temp++;
	    			cur = p[cur];
	    		}
	    		for (auto num : now) {
	    			ans[num] = temp;
	    		}
	    	}
    	}

    	for (int i = 1; i <= n; i++) {
    		cout << ans[i] << " ";
    	}
    	cout << "\n";
    }
}


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

int main() { // debug different answer online
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int q;
    cin >> q;

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

    	int p[n + 1];
    	int ans[n + 1];

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

    	for (int i = 1; i <= n; i++) {
    		cout << p[i];
    	}

    	bool check[n + 1];
    	for (int i = 1; i <= n; i++) {
    		check[n + 1] = false;
    	}
    	int temp;
    	int cur;

    	for (int i = 1; i <= n; i++) {
    		if (!check[i]) {
	    		vector<int> now;
	    		temp = 1;
	    		now.push_back(i);
	    		cur = p[i];
	    		while (cur != i) {
	    			check[cur] = true;
	    			now.push_back(cur);
	    			temp++;
	    			cur = p[cur];
	    		}
	    		for (auto num : now) {
	    			ans[num] = temp;
	    		}
	    	}
    	}

    	for (int i = 1; i <= n; i++) {
    		cout << ans[i] << " ";
    	}
    	cout << "\n";
    }
}


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

#define ll long long

typedef pair<int,int> pairs;

typedef tuple<int,int,int> tri;

int main() {

    ll q;
    cin>>q;

    //dp
    while(q--)
    {
        ll n;
        cin>>n;

      vector<ll> arr(1000000,0);

      ll dp[n]={0};

      ll p[n];

      for(ll i=0;i<n;i++)
      {
          cin>>p[i];

      }

      ll out[n];

      ll count=0;

      ll temp=0,now=0;

      for(ll i=0;i<n;i++)
      {
          temp=i;
          now=-1;
          count=0;

          if (dp[p[i]-1]==0)
          {
          while(now!=i)
          {
              if (dp[p[temp]-1]==0)
              {
              now=p[temp]-1;

              temp=now;
              count++;
              }
              else
              {
                  count+=dp[p[temp]-1]-1;
                  now=i;
              }
          }

          dp[i]=count;
          dp[p[i]-1]=dp[i];

          out[i]=dp[i];

          }
          else
          {
              out[i]=dp[p[i]-1];
          }

          //out[i]=count;

      }

        for(ll i=0;i<n;i++)
        cout<<out[i]<<" ";
        cout<<endl;

    }

	return 0;
}


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

#define ll long long

typedef pair<int,int> pairs;

typedef tuple<int,int,int> tri;

int main() {

    ll q;
    cin>>q;

    //dp
    while(q--)
    {
        ll n;
        cin>>n;

      vector<ll> arr(1000000,0);

      ll dp[n]={0};

      ll p[n];

      for(ll i=0;i<n;i++)
      {
          cin>>p[i];

      }

      ll out[n];

      ll count=0;

      ll temp=0,now=-1;

      vector<unordered_set<ll>> vec;
      vector<unordered_set<ll>>::iterator itr;
      unordered_set<ll> ptr;

      for(ll i=0;i<n;i++)
      {
          for (itr=vec.begin();itr!=vec.end();itr++)
          {

              if ((*itr).find(i)!=(*itr).end())
              {
                  break;
              }

          }

         temp=i;
          now=-1;

          if ((itr)==vec.end())
          {

          while(now!=i)
          {
              now=p[temp]-1;
              ptr.insert(now);
              //vec.push_back();

              temp=now;
              count++;

          }

          ptr.insert(i);

          vec.push_back(ptr);

          out[i]=ptr.size();

          ptr.clear();
          }
          else
          {
              out[i]=(*itr).size();
          }

    }

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

	return 0;
}


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

#define ll long long

typedef pair<int,int> pairs;

typedef tuple<int,int,int> tri;

int main() {

    ll q;
    cin>>q;

    //dp
    while(q--)
    {
        ll n;
        cin>>n;

      vector<ll> arr(1000000,0);

      ll dp[n]={0};

      ll p[n];

      for(ll i=0;i<n;i++)
      {
          cin>>p[i];

      }

      ll out[n];

      ll count=0;

      ll temp=0,now=-1;

      vector<unordered_set<ll>> vec;
      vector<unordered_set<ll>>::iterator itr;
      unordered_set<ll> pre;

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

         temp=i;
          now=-1;

          if (pre.find(i)==pre.end())
          {
              unordered_set<ll> ptr;

          while(now!=i)
          {
              now=p[temp]-1;
              ptr.insert(now);
              //vec.push_back();
              pre.insert(now);
              temp=now;
              count++;

          }

          ptr.insert(i);

          vec.push_back(ptr);

          out[i]=ptr.size();

         // ptr.clear();
          }
          else
          {
              for (itr=vec.begin();itr!=vec.end();itr++)
          {

              if ((*itr).find(i)!=(*itr).end())
              {
                  break;
              }

          }
              out[i]=(*itr).size();
          }

    }

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

	return 0;
}


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

#define ll long long

typedef pair<int,int> pairs;

typedef tuple<int,int,int> tri;

int main() {

    ll q;
    cin>>q;

    //dp
    while(q--)
    {
        ll n;
        cin>>n;

      vector<ll> arr(1000000,0);

      ll dp[n]={0};

      ll p[n];

      for(ll i=0;i<n;i++)
      {
          cin>>p[i];

      }

      ll out[n];

      ll count=0;

      ll temp=0,now=-1;

      unordered_set<ll>::iterator it;

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

         temp=i;
          now=-1;

          if (arr[i]==0)
          {
             // vector<ll> ptr;
             unordered_set<ll> pre;
              count=0;
          while(now!=i)
          {
              now=p[temp]-1;
            //  ptr.push_back(now);
              //vec.push_back();
              pre.insert(now);
              temp=now;
              count++;

          }
          //ptr.push_back(i);
          pre.insert(i);

          //vec.push_back(ptr);
          ll isz=count+1;
          for (it=pre.begin();it!=pre.end();++it)
          {

          arr[*it]=isz-1;

          }

         // ptr.clear();
          }

    }

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

	return 0;
}


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

#define ll long long

typedef pair<int,int> pairs;

typedef tuple<int,int,int> tri;

int main() {

    ll q;
    cin>>q;

    //dp
    while(q--)
    {
        ll n;
        cin>>n;

      ll arr[n]={0};

      ll p[n];

      for(ll i=0;i<n;i++)
      {
          cin>>p[i];

      }

      ll out[n];

      ll count=0;

      ll temp=0,now=-1;

      unordered_set<ll>::iterator it;

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

         temp=i;
          now=-1;

          if (arr[i]==0)
          {
             // vector<ll> ptr;
             unordered_set<ll> pre;
              count=0;
          while(now!=i)
          {
              now=p[temp]-1;
            //  ptr.push_back(now);
              //vec.push_back();
              pre.insert(now);
              temp=now;
              count++;

          }
          //ptr.push_back(i);
          pre.insert(i);

          //vec.push_back(ptr);
          ll isz=count+1;
          for (it=pre.begin();it!=pre.end();++it)
          {

          arr[*it]=isz-1;

          }

         // ptr.clear();
          }

    }

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

	return 0;
}


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

#define ll long long
#define push push_back
#define pop pop_back

int main() {
    ll q;
    cin>>q;
    while(q--)
    {
        ll n;
        cin>>n;
        ll p[n];
        for(ll i=0;i<n;i++)
            cin>>p[i];
        ll a[n]={0};
        unordered_set<ll> checked;

        for(ll i=0;i<n;i++)
        {
            unordered_set<ll> toinsert;
            ll loopcount=0;
            if(checked.find(i) == checked.end())
            {
                ll temp=i;
                do
                {
                    ll current=temp;
                    temp=p[current]-1;
                    loopcount++;
                    // cout<<current<<" "<<temp<<" "<<loopcount<<'\n';
                    checked.insert(current);
                    toinsert.insert(current);
                }
                while(temp!=i);
                unordered_set<ll> :: iterator it;
                for(it=toinsert.begin();it!=toinsert.end();it++)
                {
                    a[*it]=loopcount;
                }
            }

        }
        for(ll i=0;i<n;i++)
            cout<<a[i]<<" ";
        cout<<'\n';
    }

    return 0;
}


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

#define ll long long

typedef pair<int,int> pairs;

typedef tuple<int,int,int> tri;

int main() {

    ll q;
    cin>>q;

    //dp
    while(q--)
    {
        ll n;
        cin>>n;

      vector<ll> arr(1000000,0);

      ll dp[n]={0};

      ll p[n];

      for(ll i=0;i<n;i++)
      {
          cin>>p[i];

      }

      ll out[n];

      ll count=0;

      ll temp=0,now=-1;

      vector<unordered_set<ll>> vec;
      vector<unordered_set<ll>>::iterator itr;
      unordered_set<ll> pre;

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

         temp=i;
          now=-1;

          if (arr[i]==0)
          {
              vector<ll> ptr;

          while(now!=i)
          {
              now=p[temp]-1;
              ptr.push_back(now);
              //vec.push_back();
             // pre.insert(now);
              temp=now;
              count++;

          }
          ptr.push_back(i);
          //pre.insert(i);

          //vec.push_back(ptr);
          ll isz=ptr.size();
          for (ll j=0;j<isz;j++)
          {
          arr[ptr[j]]=isz-1;
          }

         // ptr.clear();
          }

    }

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

	return 0;
}


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

#define ll long long

typedef pair<int,int> pairs;

typedef tuple<int,int,int> tri;

int main() {

    ll q;
    cin>>q;

    //dp
    while(q--)
    {
        ll n;
        cin>>n;

      vector<ll> arr(1000000,0);

      ll dp[n]={0};

      ll p[n];

      for(ll i=0;i<n;i++)
      {
          cin>>p[i];

      }

      ll out[n];

      ll count=0;

      ll temp=0,now=-1;

      unordered_set<ll> pre;

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

         temp=i;
          now=-1;

          if (arr[i]==0)
          {
              vector<ll> ptr;
              count=0;
          while(now!=i)
          {
              now=p[temp]-1;
              ptr.push_back(now);
              //vec.push_back();
             // pre.insert(now);
              temp=now;
              count++;

          }
          ptr.push_back(i);
          //pre.insert(i);

          //vec.push_back(ptr);
          ll isz=count+1;
          for (ll j=0;j<isz;j++)
          {

          arr[ptr[j]]=isz-1;

          }

         // ptr.clear();
          }

    }

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

	return 0;
}


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

#define ll long long

typedef pair<int,int> pairs;

typedef tuple<int,int,int> tri;

int main() {

    ll q;
    cin>>q;

    //dp
    while(q--)
    {
        ll n;
        cin>>n;

      vector<ll> arr(1000000,0);

      ll dp[n]={0};

      ll p[n];

      for(ll i=0;i<n;i++)
      {
          cin>>p[i];

      }

      ll out[n];

      ll count=0;

      ll temp=0,now=0;

      for(ll i=0;i<n;i++)
      {
          temp=i;
          now=-1;
          count=0;

          while(now!=i)
          {
              now=p[temp]-1;
              temp=now;
              count++;
          }

          out[i]=count;

      }

        for(ll i=0;i<n;i++)
        cout<<out[i]<<" ";
        cout<<endl;

    }

	return 0;
}


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

#define ll long long

typedef pair<int,int> pairs;

typedef tuple<int,int,int> tri;

int main() {

    ll q;
    cin>>q;

    //dp
    while(q--)
    {
        ll n;
        cin>>n;

      vector<ll> arr(1000000,0);

      ll dp[n]={0};

      ll p[n];

      for(ll i=0;i<n;i++)
      {
          cin>>p[i];

      }

      ll out[n];

      ll count=0;

      ll temp=0,now=0;

      for(ll i=0;i<n;i++)
      {
          temp=i;
          now=-1;
          count=0;

          if (dp[p[i]-1]==0)
          {
          while(now!=i)
          {
              now=p[temp]-1;
              temp=now;
              count++;
          }

          dp[i]=count;
          dp[p[i]-1]=dp[i];

          out[i]=dp[i];

          }
          else
          {
              out[i]=dp[p[i]-1];
          }

          //out[i]=count;

      }

        for(ll i=0;i<n;i++)
        cout<<out[i]<<" ";
        cout<<endl;

    }

	return 0;
}


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

#define ll long long

typedef pair<int,int> pairs;

typedef tuple<int,int,int> tri;

int main() {

    ll q;
    cin>>q;

    //dp
    while(q--)
    {
        ll n;
        cin>>n;

      vector<ll> arr(1000000,0);

      ll dp[n]={0};

      ll p[n];

      for(ll i=0;i<n;i++)
      {
          cin>>p[i];

      }

      ll out[n];

      ll count=0;

      ll temp=0,now=-1;

      vector<unordered_set<ll>> vec;
      vector<unordered_set<ll>>::iterator itr;
      unordered_set<ll> pre;

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

         temp=i;
          now=-1;

          if (arr[i]==0)
          {
              vector<ll> ptr;

          while(now!=i)
          {
              now=p[temp]-1;
              ptr.push_back(now);
              //vec.push_back();
              pre.insert(now);
              temp=now;
              count++;

          }
          ptr.push_back(i);
          pre.insert(i);

          //vec.push_back(ptr);
          for (ll j=0;j<ptr.size();j++)
          {
          arr[ptr[j]]=ptr.size()-1;
          }

         // ptr.clear();
          }

    }

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

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int fa[maxn], cnt[maxn];
int find(int x) {
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}
int main() {
	int T, n, x;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) fa[i] = i;
		for (int i = 1; i <= n; i++) cnt[i] = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%d", &x);
			int u = find(x);
			int v = find(i);
			fa[u] = v;
		}
		for (int i = 1; i <= n; i++) cnt[find(i)]++;
		for (int i = 1; i <= n; i++) printf("%d ", cnt[find(i)]);
		printf("\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int fa[maxn], cnt[maxn];
int find(int x) {
	return x == fa[x] ? x : find(fa[x]);
}
int main() {
	int T, n, x;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) fa[i] = i;
		for (int i = 1; i <= n; i++) cnt[i] = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%d", &x);
			int u = find(x);
			int v = find(i);
			fa[u] = v;
		}
		for (int i = 1; i <= n; i++) cnt[find(i)]++;
		for (int i = 1; i <= n; i++) printf("%d ", cnt[find(i)]);
		printf("\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int fa[maxn], cnt[maxn], a[maxn];
int find(int x) {
	return x == fa[x] ? x : find(fa[x]);
}
int main() {
	int T, n;;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) fa[i] = i;
		for (int i = 1; i <= n; i++) cnt[i] = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
			int u = find(i);
			int v = find(a[i]);
			if(u != v)fa[u] = v;
		}
		for (int i = 1; i <= n; i++) find(i);
		for (int i = 1; i <= n; i++) cnt[find(i)]++;
		for (int i = 1; i <= n; i++) printf("%d ", cnt[find(i)]);
		printf("\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int fa[maxn], cnt[maxn];
int find(int x) {
	return x == fa[x] ? x : find(fa[x]);
}
int main() {
	int T, n, x;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) fa[i] = i;
		for (int i = 1; i <= n; i++) cnt[i] = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%d", &x);
			int u = find(x);
			int v = find(i);
			if (u != v) fa[u] = v;
		}
		for (int i = 1; i <= n; i++) cnt[find(i)]++;
		for (int i = 1; i <= n; i++) printf("%d ", cnt[find(i)]);
		printf("\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int fa[maxn], cnt[maxn], a[maxn];
int find(int x) {
	return x == fa[x] ? x : find(fa[x]);
}
int main() {
	int T, n;;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) fa[i] = i;
		for (int i = 1; i <= n; i++) cnt[i] = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
			int u = find(i);
			int v = find(a[i]);
			fa[u] = v;
		}
		for (int i = 1; i <= n; i++) find(i);
		for (int i = 1; i <= n; i++) cnt[find(i)]++;
		for (int i = 1; i <= n; i++) printf("%d ", cnt[find(i)]);
		printf("\n");
	}
	return 0;
}


#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll counter;
ll a[200005];
ll ans[200005];
ll findset(ll x,ll y){
if(a[x]==y){
    return ans[x]=counter;
}
else {
        counter++;
     return ans[x]=findset(a[x],y);
}
}
int main()
{   ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        //ll a[n];
        memset(ans,0,sizeof(ans));
        for(ll i=1;i<=n;i++){
                cin>>a[i];
        ans[i]=1;}
        for(ll i=0;i<n;i++){
                counter=1;
            ans[i+1]=findset(i+1,i+1);
            //cout<<counter<<" ";
        }
        for(ll i=1;i<=n;i++)cout<<ans[i]<<" ";
        cout<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll counter;
ll a[200005];
ll ans[200005];
ll findset(ll x,ll y){
if(a[x]==y){
    return ans[x]=counter;
}
else {
        counter++;
     return findset(a[x],y);
}
}
int main()
{   ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        //ll a[n];
        memset(ans,0,sizeof(ans));
        for(ll i=1;i<=n;i++){
                cin>>a[i];
        ans[i]=1;}
        for(ll i=0;i<n;i++){
                counter=1;
            ans[i+1]=findset(i+1,i+1);
            //cout<<counter<<" ";
        }
        for(ll i=1;i<=n;i++)cout<<ans[i]<<" ";
        cout<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll counter;
ll a[200005];
ll ans[200005];
unordered_map<ll,ll> mp;
ll find(ll x,ll y)
{
	if(mp[x]==y)
		return ans[y];
	return ans[y]=find(mp[x],y)+1;
}
int main()
{   ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        //ll a[n];
        memset(ans,0,sizeof(ans));
        for(ll i=1;i<=n;i++){
                cin>>a[i];
                mp[i]=a[i];
        ans[i]=1;}
        for(ll i=0;i<n;i++){
                counter=1;
            ans[i+1]=find(i+1,i+1);
            //cout<<counter<<" ";
        }
        for(ll i=1;i<=n;i++)cout<<ans[i]<<" ";
        cout<<endl;
        mp.clear();
    }

    return 0;
}


#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll a[200005];
ll parent[500005];
ll findset(ll x){
if(x==parent[x])return x;
else return parent[x]=findset(parent[x]);
}
void unite(ll a,ll b){
	a=findset(a);
	b=findset(b);
	parent[a]=b;
}
int main()
{   ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        for(ll i=1;i<=n;i++){
            cin>>a[i];
            parent[i]=i;
        }
        for(ll i=1;i<=n;i++){
            if(findset(i)!=findset(a[i]))unite(i,a[i]);
        }
        ll ans[n+1]={0};
        for(ll i=1;i<=n;i++){
            ans[findset(i)]++;
        }
        for(ll i=1;i<=n;i++){
            cout<<ans[findset(i)]<<" ";
        }
        cout<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll counter;
ll a[200005];
ll ans[200005];
unordered_map<ll,ll> mp;
ll find(ll x,ll y)
{
	while(mp[x]!=y){
        counter++;
        x=mp[x];
	}
	return counter;
}
int main()
{   ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        //ll a[n];
        memset(ans,0,sizeof(ans));
        for(ll i=1;i<=n;i++){
                cin>>a[i];
                mp[i]=a[i];
        ans[i]=1;}
        for(ll i=0;i<n;i++){
                counter=1;
            ans[i+1]=find(i+1,i+1);
            //cout<<counter<<" ";
        }
        for(ll i=1;i<=n;i++)cout<<ans[i]<<" ";
        cout<<endl;
        mp.clear();
    }

    return 0;
}


#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll counter;
ll a[200005];
void findset(ll x,ll n,ll y){
if(a[x]==y){
    return;
}
else {
    counter++;
     findset(a[x],n,y);
}
}
int main()
{   ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        //ll a[n];
        for(ll i=1;i<=n;i++)cin>>a[i];
        for(ll i=0;i<n;i++){
            counter=1;
            findset(i+1,n,i+1);
            cout<<counter<<" ";
        }
        cout<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll counter;
ll a[200005];
ll ans[200005];
ll findset(ll x,ll y){
if(a[x]==y){
    return ans[y]=counter;
}
else {
        counter++;
     return ans[y]=findset(a[x],y);
}
}
int main()
{   ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        //ll a[n];
        memset(ans,0,sizeof(ans));
        for(ll i=1;i<=n;i++){
                cin>>a[i];
        ans[i]=1;}
        for(ll i=0;i<n;i++){
                counter=1;
            ans[i+1]=findset(i+1,i+1);
            //cout<<counter<<" ";
        }
        for(ll i=1;i<=n;i++)cout<<ans[i]<<" ";
        cout<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        int n;
        cin>>n;
        int p[n];
        int i;
        for(i=0;i<n;i++)
            cin>>p[i];
        int days[n];
        for(i=0;i<n;i++)
        {
            days[i]=1;
            p[i]--;
        }
        int flag[n];
        memset(flag,0,sizeof(flag));
        for(i=0;i<n;i++)
        {
            if(flag[i]==1)
                continue;
            int j=p[i];
            vector <int> v;
            while(j!=i)
            {
                v.push_back(i);
                j=p[j];
                days[i]++;
            }
            int k=v.size()+1;
            vector <int> :: iterator it;
            for( it = v.begin(); it!=v.end();it++)
            {
                days[*it]=k;
                flag[*it]=1;
            }
        }
        for(i=0;i<n;i++)
            cout<<days[i]<<" ";
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        int n;
        cin>>n;
        int p[n];
        int i;
        for(i=0;i<n;i++)
            cin>>p[i];
        long long int days[n];
        for(i=0;i<n;i++)
        {
            days[i]=1;
            p[i]--;
        }
        //memset(days,1,sizeof(days));
        for(i=0;i<n;i++)
        {
            int j=p[i];
            if(j==i)
            continue;
            days[j]++;
        }
        for(i=0;i<n;i++)
            cout<<days[i]<<" ";
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        int n;
        cin>>n;
        int p[n];
        int i;
        for(i=0;i<n;i++)
            cin>>p[i];
        int days[n];
        for(i=0;i<n;i++)
        {
            days[i]=1;
            p[i]--;
        }
        //memset(days,1,sizeof(days));
        for(i=0;i<n;i++)
        {
            int j=p[i];
            vector <int> v;
            while(j!=i)
            {
                v.push_back(i);
                j=p[j];
                days[i]++;
            }
            int k=v.size()+1;
            vector <int> :: iterator it;
            for( it = v.begin(); it!=v.end();it++)
                days[*it]=k;
        }
        for(i=0;i<n;i++)
            cout<<days[i]<<" ";
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        int n;
        cin>>n;
        int p[n];
        int i;
        for(i=0;i<n;i++)
            cin>>p[i];
        int days[n];
        for(i=0;i<n;i++)
        {
            days[i]=1;
            p[i]--;
        }
        int flag[n];
        memset(flag,0,sizeof(flag));
        for(i=0;i<n;i++)
        {
            if(flag[i]==1)
                continue;
            int j=p[i];
            vector <int> v;
            while(j!=i)
            {
                v.push_back(i);
                j=p[j];
            }
            int k=v.size()+1;
            vector <int> :: iterator it;
            for( it = v.begin(); it!=v.end();it++)
            {
                days[*it]=k;
                flag[*it]=1;
            }
        }
        for(i=0;i<n;i++)
            cout<<days[i]<<" ";
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
	int q;
	cin >> q;
	while(q--) {
		int n;
		cin >> n;
		int p[n];
		for (int j = 0; j < n; ++j) {
			cin >> p[j];
			p[j]--;
		}
		vector<int> flag(n);
		int days[n];
		for (int j = 0; j < n; ++j) {
			if (!flag[j]) {
				vector<int> v;
				while (!flag[j]) {
					v.push_back(j);
					flag[j] = true;
					j = p[j];
				}
				for (auto it : v) days[it] = v.size();
			}
		}
		for (int j = 0; j < n; ++j) cout << days[j] << " ";
		cout << endl;
	}

	return 0;
}


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

typedef long long ll;

const int MOD = 1e9 + 7 ;
const double PI = 2 * acos(0.0);
ll val = 0 ;

bool visit[200005] ;
ll freq[200005], arr[200005] ;

void check(ll pos, ll cnt)
{
  if(!visit[pos]) {
    visit[pos] = true ;
    check(arr[pos], cnt + 1) ;
    freq[pos] = val ;
  }
  else {
    val = max(val, cnt) ;
    return ;
  }
}

int main()
{
  ll t ;
  cin >> t ;
  while(t--) {
    ll n ;
    cin >> n ;
    for(int i = 1; i <= n; i++) {
      cin >> arr[i] ;
      visit[i] = false ;
    }

    for(int i = 1; i <= n; i++) {
      val = 0 ;
      if(!visit[i]) {
        visit[i] = true ;
        check(arr[i], 1) ;
        freq[i] = val ;
      }
    }

    for(int i = 1; i <= n; i++) {
      cout << freq[i] << " " ;
    }
    cout << endl ;

  }
}


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

typedef long long ll;
ll arr[200005], arr2[200005] ;
ll MOD = 1e9 + 7;

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

  while(t--){
    ll n, ans = 0;
    cin>>n;
    map<ll, ll> mp1;
    for(int i = 0; i < n; i++){
      arr2[i] = 0;
    }
    for(int i = 0; i < n; i++)  cin>>arr[i];
    ll val = 1;
    for(int i = 0; i < n; i++){
      if(arr2[i] != 0){ cout<<arr2[i]<<" "; continue;}
      ll start = arr[i] ;
      ll ans = 1;
      while(start != i + 1){
        start = arr[start - 1];
        ans++;
      }
      arr2[i] = ans;
      start = arr[i];
      while(start != i + 1){
        arr2[start-1] = ans;
        start = arr[start - 1];
      }
      cout<<ans<<" ";
    }
    cout<<endl;
  }
}


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

typedef long long ll;
ll arr[20000005] ;
ll MOD = 1e9 + 7;

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

  while(t--){
    ll n, ans = 0;
    cin>>n;
    map<ll, ll> mp1;
    arr[n];
    for(int i = 0; i < n; i++)  cin>>arr[i];
    ll val = 1;
    for(int i = 0; i < n; i++){
      ll start = arr[i] ;
      ll ans = 1;
      while(start != i + 1){
        start = arr[start - 1];
        ans++;
      }
      cout<<ans<<" ";
    }
    cout<<endl;
  }
}


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

typedef long long ll;
ll arr[20000005], arr2[20000005] ;
ll MOD = 1e9 + 7;

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

  while(t--){
    ll n, ans = 0;
    cin>>n;
    map<ll, ll> mp1;
    for(int i = 0; i < n; i++){
      arr2[i] = 0;
    }
    for(int i = 0; i < n; i++)  cin>>arr[i];
    ll val = 1;
    for(int i = 0; i < n; i++){
      if(arr2[i] != 0)  continue;
      ll start = arr[i] ;
      ll ans = 1;
      while(start != i + 1){
        start = arr[start - 1];
        ans++;
      }
      arr2[i] = ans;
      while(start != i + 1){
        start = arr[start - 1];
        arr2[start] = ans;
      }
      cout<<ans<<" ";
    }
    cout<<endl;
  }
}


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

typedef long long ll;
ll arr[200005], arr2[200005] ;
ll MOD = 1e9 + 7;

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

  while(t--){
    ll n, ans = 0;
    cin>>n;
    map<ll, ll> mp1;
    for(int i = 0; i < n; i++){
      arr2[i] = 0;
    }
    for(int i = 0; i < n; i++)  cin>>arr[i];
    ll val = 1;
    for(int i = 0; i < n; i++){
      if(arr2[i] != 0)  continue;
      ll start = arr[i] ;
      ll ans = 1;
      while(start != i + 1){
        start = arr[start - 1];
        ans++;
      }
      arr2[i] = ans;
      while(start != i + 1){
        start = arr[start - 1];
        arr2[start-1] = ans;
      }
      cout<<ans<<" ";
    }
    cout<<endl;
  }
}


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

typedef long long ll;
ll arr[200005], arr2[200005] ;
ll MOD = 1e9 + 7;

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

  while(t--){
    ll n, ans = 0;
    cin>>n;
    map<ll, ll> mp1;
    for(int i = 0; i < n; i++){
      arr2[i] = 0;
    }
    for(int i = 0; i < n; i++)  cin>>arr[i];
    ll val = 1;
    for(int i = 0; i < n; i++){
      if(arr2[i] != 0)  continue;
      ll start = arr[i] ;
      ll ans = 1;
      while(start != i + 1){
        start = arr[start - 1];
        ans++;
      }
      arr2[i] = ans;
      while(start != i + 1){
        start = arr[start - 1];
        arr2[start] = ans;
      }
      cout<<ans<<" ";
    }
    cout<<endl;
  }
}


#include<bits/stdc++.h>
using namespace std;
int ar2[200001];
int parent[200001];
int find(int a)
{

  if(ar2[a]<0)
  return a;
  else
  find(ar2[a]);
}
void Union(int a,int b)
{

 int c=find(a);
        int d=find(b);

    if(c==d)
    return;
    else
    {

        ar2[c]+=ar2[d];
        ar2[d]=c;
    }
    return;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long int q,n;
    cin>>q;
    while(q--)
    {
        cin>>n;
        int ar[n+1];//original values

        for(int i=1;i<=n;i++){
        cin>>ar[i];
          ar2[i]=-1;

        }
        for(int i=1;i<=n;i++)
         Union(ar[i],i);

       for(int i=1;i<=n;i++)
       cout<<-1*(ar2[find(i)])<<" ";
       cout<<"\n";
    }

}


#include<bits/stdc++.h>
using namespace std;
int ar2[200001];
int find(int a)
{

  if(ar2[a]<0)
  return a;
  else
  find(ar2[a]);
}
void Union(int a,int b)
{

    if(find(a)==find(b))
    return;
    else
    {
        int c=find(a);
        int d=find(b);
        ar2[c]+=ar2[d];
        ar2[d]=c;
    }
    return;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long int q,n;
    cin>>q;
    while(q--)
    {
        cin>>n;
        int ar[n+1];
        // int ans[n+1];

        for(int i=1;i<=n;i++){
        cin>>ar[i];
          ar2[i]=-1;

        }
        for(int i=1;i<=n;i++)
         Union(ar[i],i);

       for(int i=1;i<=n;i++)
       cout<<-1*(ar2[find(i)])<<" ";
       cout<<"\n";
    }

}


#include<bits/stdc++.h>
using namespace std;
int ar2[200001];
int parent[200001];
int find(int a)
{

  if(ar2[a]<0)
  return a;
  else
  return find(ar2[a]);
}
void Union(int a,int b)
{

 int c=find(a);
        int d=find(b);

    if(c==d)
    return;

        ar2[c]+=ar2[d];
        ar2[d]=c;

    return;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long int q,n;
    cin>>q;
    while(q--)
    {
        cin>>n;
        int ar[n+1];//original values

        for(int i=1;i<=n;i++){
        cin>>ar[i];
          ar2[i]=-1;

        }
        for(int i=1;i<=n;i++)
         Union(ar[i],i);

       for(int i=1;i<=n;i++)
       cout<<-1*(ar2[find(i)])<<" ";
       cout<<"\n";
    }

}


#include<bits/stdc++.h>
using namespace std;
int ar2[300001];
int find(int a)
{
    while(ar2[a]>0)
    a=ar2[a];

    return a;
}
void Union(int a,int b)
{

    if(find(a)==find(b))
    return;
    else
    {

        ar2[find(a)]+=ar2[find(b)];
        ar2[find(b)]=find(a);
    }
    return;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long int q,n;
    cin>>q;
    while(q--)
    {
        cin>>n;
        int ar[n+1];//original values
        int ans[n+1];
        // int ar2[n+1];
        // for(int i=1;i<=n;i++)

        for(int i=1;i<=n;i++){
        cin>>ar[i];
          ar2[i]=-1;

        }
        for(int i=1;i<=n;i++)
         Union(ar[i],i);

       for(int i=1;i<=n;i++)

       ans[i]=-1*(ar2[find(i)]);

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

}


#include<bits/stdc++.h>
using namespace std;
int ar2[200001];
int find(int a)
{

  if(ar2[a]<0)
  return a;
  else
  find(ar2[a]);
}
void Union(int a,int b)
{

    if(find(a)==find(b))
    return;
    else
    {
        int c=find(a);
        int d=find(b);
        ar2[c]+=ar2[d];
        ar2[d]=c;
    }
    return;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long int q,n;
    cin>>q;
    while(q--)
    {
        cin>>n;
        int ar[n+1];//original values
        int ans[n+1];

        for(int i=1;i<=n;i++){
        cin>>ar[i];
          ar2[i]=-1;

        }
        for(int i=1;i<=n;i++)
         Union(ar[i],i);

       for(int i=1;i<=n;i++)

       ans[i]=-1*(ar2[find(i)]);

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

}


#include<bits/stdc++.h>
using namespace std;
int ar2[200001];
int parent[200001];
int find(int a)
{

  if(ar2[a]<0)
  return a;
  else
  return ar2[a]=find(ar2[a]);
}
void Union(int a,int b)
{

 int c=find(a);
        int d=find(b);

    if(c==d)
    return;

        ar2[c]+=ar2[d];
        ar2[d]=c;

    return;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long int q,n;
    cin>>q;
    while(q--)
    {
        cin>>n;
        int ar[n+1];//original values

        for(int i=1;i<=n;i++){
        cin>>ar[i];
          ar2[i]=-1;

        }
        for(int i=1;i<=n;i++)
         Union(ar[i],i);

       for(int i=1;i<=n;i++)
       cout<<-1*(ar2[find(i)])<<" ";
       cout<<"\n";
    }

}


#include<bits/stdc++.h>
using namespace std;
clock_t start,end1;
double time_used;
long long find(long long arr[],long long a)
{
    long long index=a;
    long long temp=a;
    while(arr[a]!=a)
    {
        a=arr[a];
    }
    while(arr[temp]!=a)
    {
        temp=arr[temp];
        arr[index]=a;
        index=temp;
    }
    return a;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    start=clock();
    long long t;
    cin>>t;
    while(t--)
    {
        long long n;
        cin>>n;
        long long arr[n];
        long long set[n];
        long long size[n];
        for(long long i=0;i<n;++i)
        {
            cin>>arr[i];
            arr[i]--;
            set[i]=i;
            size[i]=1;
        }
        //set contains parent
        long long a,b;
        for(long long i=0;i<n;++i)
        {
            a=find(set,arr[i]);
            b=find(set,i);
            if(a==b)
            {
                continue;
            }
            else
            {
                if(size[a]>size[b])
                {
                    set[b]=set[a];
                    size[a]=size[a]+size[b];
                }
                else
                {
                    set[a]=set[b];
                    size[b]=size[b]+size[a];
                }
            }
        }
        long long x;
        for(long long i=0;i<n;++i)
        {
            x=find(set,arr[i]);
            cout<<size[x]<<" ";
        }
        cout<<endl;
    }
    end1=clock();
    time_used=((double)(end1-start))/CLOCKS_PER_SEC;
    //cout<<"Time Taken :"<<fixed<<setprecision(15)<<time_used<<endl;
    cout<<endl;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    long long t;
    cin>>t;
    while(t--)
    {
        long long n;
        cin>>n;
        long long arr[n];
        long long set[n];
        for(long long i=0;i<n;++i)
        {
            cin>>arr[i];
            arr[i]--;
            set[i]=i;
        }
        for(long long i=0;i<n;++i)
        {
            if(set[arr[i]]==set[i])
            {
                continue;
            }
            else
            {
                long long temp=set[arr[i]];
                for(long long j=0;j<n;++j)
                {
                    if(set[j]==temp)
                    {
                        set[j]=set[i];
                    }
                }
            }
        }
        map<long long,long long> m;
        for(long long i=0;i<n;++i)
        {
            m[set[i]]++;
        }
        for(long long i=0;i<n;++i)
        {
            cout<<m[set[i]]<<" ";
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
long long find(long long arr[],long long a)
{
    //long long index=a;
    //long long temp=a;
    while(arr[a]!=a)
    {
        a=arr[a];
    }
    //while(arr[temp]!=a)
    //{
    //    temp=arr[temp];
    //    arr[index]=a;
    //    index=temp;
    //}
    return a;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    long long t;
    cin>>t;
    while(t--)
    {
        long long n;
        cin>>n;
        long long arr[n];
        long long set[n];
        long long size[n];
        for(long long i=0;i<n;++i)
        {
            cin>>arr[i];
            arr[i]--;
            set[i]=i;
            size[i]=1;
        }
        //set contains parent
        long long a,b;
        for(long long i=0;i<n;++i)
        {
            a=find(set,arr[i]);
            b=find(set,i);
            if(a==b)
            {
                continue;
            }
            else
            {
                if(size[a]>size[b])
                {
                    set[b]=set[a];
                    size[a]=size[a]+size[b];
                }
                else
                {
                    set[a]=set[b];
                    size[b]=size[b]+size[a];
                }
            }
        }
        long long x;
        for(long long i=0;i<n;++i)
        {
            x=find(set,arr[i]);
            cout<<size[x]<<" ";
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
long long find(long long arr[], long long a)
{
         if(arr[a]==a)
         {
                 return a;
          }
         return arr[a]=find(arr[a]);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    long long t;
    cin>>t;
    while(t--)
    {
        long long n;
        cin>>n;
        long long arr[n];
        long long set[n];
        long long size[n];
        for(long long i=0;i<n;++i)
        {
            cin>>arr[i];
            arr[i]--;
            set[i]=i;
            size[i]=1;
        }
        //set contains parent
        long long a,b;
        for(long long i=0;i<n;++i)
        {
            a=find(set,arr[i]);
            b=find(set,i);
            if(a==b)
            {
                continue;
            }
            else
            {
                if(size[a]>size[b])
                {
                    set[b]=set[a];
                    size[a]=size[a]+size[b];
                }
                else
                {
                    set[a]=set[b];
                    size[b]=size[b]+size[a];
                }
            }
        }
        long long x;
        for(long long i=0;i<n;++i)
        {
            x=find(set,arr[i]);
            cout<<size[x]<<" ";
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
long long find(long long arr[],long long a)
{
    while(arr[a]!=a)
    {
        a=arr[a];
    }
    return a;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    long long t;
    cin>>t;
    while(t--)
    {
        long long n;
        cin>>n;
        long long arr[n];
        long long set[n];
        long long size[n];
        for(long long i=0;i<n;++i)
        {
            cin>>arr[i];
            arr[i]--;
            set[i]=i;
            size[i]=1;
        }
        //set contains parent
        long long a,b;
        for(long long i=0;i<n;++i)
        {
            a=find(set,arr[i]);
            b=find(set,i);
            if(a==b)
            {
                continue;
            }
            else
            {
                if(size[a]>size[b])
                {
                    set[b]=set[a];
                    size[a]=size[a]+size[b];
                }
                else
                {
                    set[a]=set[b];
                    size[b]=size[b]+size[a];
                    //cout<<size[i]<<endl;
                }
            }
        }
        //for(long long i=0;i<n;++i)
        //{
        //    cout<<size[i]<<" ";
        //}
        //cout<<endl;
        long long x;
        for(long long i=0;i<n;++i)
        {
            x=find(set,arr[i]);
            cout<<size[x]<<" ";
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    long long t;
    cin>>t;
    while(t--)
    {
        long long n;
        cin>>n;
        long long arr[n];
        for(long long i=0;i<n;++i)
        {
            cin>>arr[i];
            arr[i]--;
        }
        for(long long i=0;i<n;++i)
        {
            long long k=i;
            long long count=0;
            while(i!=arr[k])
            {
                //cout<<k<<" "<<arr[k]<<endl;
                count++;
                k=arr[k];
            }
            cout<<count+1<<" ";
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
long long find(long long arr[],long long a)
{
    long long index=a;
    long long temp=a;
    while(arr[a]!=a)
    {
        a=arr[a];
    }
    while(arr[temp]!=a)
    {
        temp=arr[temp];
        arr[index]=a;
        index=temp;
    }
    return a;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    long long t;
    cin>>t;
    while(t--)
    {
        long long n;
        cin>>n;
        long long arr[n];
        long long set[n];
        long long size[n];
        for(long long i=0;i<n;++i)
        {
            cin>>arr[i];
            arr[i]--;
            set[i]=i;
            size[i]=1;
        }
        //set contains parent
        long long a,b;
        for(long long i=0;i<n;++i)
        {
            a=find(set,arr[i]);
            b=find(set,i);
            if(a==b)
            {
                continue;
            }
            else
            {
                if(size[a]>size[b])
                {
                    set[b]=set[a];
                    size[a]=size[a]+size[b];
                }
                else
                {
                    set[a]=set[b];
                    size[b]=size[b]+size[a];
                }
            }
        }
        long long x;
        for(long long i=0;i<n;++i)
        {
            x=find(set,arr[i]);
            cout<<size[x]<<" ";
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    long long t;
    cin>>t;
    while(t--)
    {
        long long n;
        cin>>n;
        long long arr[n];
        long long set[n];
        for(long long i=0;i<n;++i)
        {
            cin>>arr[i];
            arr[i]--;
        }
        //make set
        for(long long i=0;i<n;++i)
        {
            set[i]=i;
        }
        for(long long i=0;i<n;++i)
        {
            if(set[i]==set[arr[i]])
            {
                continue;
            }
            else
            {
                for(long long j=0;j<n;++j)
                {
                    if(set[j]==set[arr[i]])
                    {
                        set[j]=set[i];
                    }
                }
            }
        }
        map<long long,long long>m;
        for(long long i=0;i<n;++i)
        {
            m[set[i]]++;
        }
        for(long long i=0;i<n;++i)
        {
            cout<<m[set[i]]<<" ";
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
clock_t start,end1;
double time_used;
long long find(long long arr[],long long a)
{
    //long long index=a;
    //long long temp=a;
    while(arr[a]!=a)
    {
        a=arr[a];
    }
    //while(arr[temp]!=a)
    //{
    //    temp=arr[temp];
    //    arr[index]=a;
    //    index=temp;
    //}
    return a;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    start=clock();
    long long t;
    cin>>t;
    while(t--)
    {
        long long n;
        cin>>n;
        long long arr[n];
        long long set[n];
        long long size[n];
        for(long long i=0;i<n;++i)
        {
            cin>>arr[i];
            arr[i]--;
            set[i]=i;
            size[i]=1;
        }
        //set contains parent
        long long a,b;
        for(long long i=0;i<n;++i)
        {
            a=find(set,arr[i]);
            b=find(set,i);
            if(a==b)
            {
                continue;
            }
            else
            {
                if(size[a]>size[b])
                {
                    set[b]=set[a];
                    size[a]=size[a]+size[b];
                }
                else
                {
                    set[a]=set[b];
                    size[b]=size[b]+size[a];
                }
            }
        }
        long long x;
        for(long long i=0;i<n;++i)
        {
            x=find(set,arr[i]);
            cout<<size[x]<<" ";
        }
        cout<<endl;
    }
    end1=clock();
    time_used=((double)(end1-start))/CLOCKS_PER_SEC;
    //cout<<"Time Taken :"<<fixed<<setprecision(15)<<time_used<<endl;
    cout<<endl;

}


/*input
6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3
*/

#include<bits/stdc++.h>
#define printarr(a,n) for(int i=0;i<n;i++) {cout << a[i] << " ";} cout << "\n"
#define getarr(a,n) for(int i=0;i<n;i++) cin >> a[i]
#define deb2d(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++) cout << setw(3) << a[i][j] << " "; cout << "\n";} cout << "\n\n";cin.ignore();
#define deb(x) cout << x << " ";
#define pb(x) push_back(x)
#define vi vector<int>
#define pii pair<int,int>
#define int long long int
#define endl "\n"
#define MOD 1000000007
#define bak cout << "Blah\n"
using namespace std;
const int MAX = (int) 1e5
vi res(2*MAX + 5);

void dfs(int i, vi a, int n){
	vi visited(n);
	int cnt = 0;
	while(visited[i] == 0){
		visited[i] = 1;
		i = a[i];
		cnt++;
	}
	for(int i = 0; i < n; ++i){
		if(visited[i]) res[i] = cnt;
	}
}

void executor(int test_case){
	int n,k;
	cin >> n;
	vi a(n);
	//bak;
	//printarr(res,n);
	for(auto &itr:a){
		cin >> itr;
		--itr;
	}
	for(int i = 0; i < n; ++i){
		if(res[i] == 0){
			dfs(i,a,n);
		}
	}
	printarr(res,n);
	for(int i = 0; i < n; ++i){
		res[i] = 0;
	}
}

int32_t main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int t = 1;
	cin >> t;
	for(int i = 1 ; i <= t ; i++) executor(i);
}


/*input
6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3
*/

#include<bits/stdc++.h>
#define printarr(a,n) for(int i=0;i<n;i++) {cout << a[i] << " ";} cout << "\n"
#define getarr(a,n) for(int i=0;i<n;i++) cin >> a[i]
#define deb2d(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++) cout << setw(3) << a[i][j] << " "; cout << "\n";} cout << "\n\n";cin.ignore();
#define deb(x) cout << x << " ";
#define pb(x) push_back(x)
#define vi vector<int>
#define pii pair<int,int>
#define endl "\n"
#define MOD 1000000007
#define bak cout << "Blah\n"
using namespace std;
const int MAX = (int) 1e5;
vi res(2*MAX + 5);
map<int,int> mp;
void dfs(int i, vi a, int n,int clr){
	vi visited(n);
	int cnt = 0;
	while(res[i] == 0){
		res[i] = clr;
		i = a[i];
		cnt++;
	}
	mp[clr] = cnt;
}

void executor(int test_case){
	mp.clear();
	int n,k;
	cin >> n;
	vi a(n);
	for(auto &itr:a){
		cin >> itr;
		--itr;
	}
	int clr = 1;
	for(int i = 0; i < n; ++i){
		if(res[i] == 0){
			dfs(i,a,n,clr);
			clr++;
		}
	}
	for(int i = 0; i < n; ++i){
		cout << mp[res[i]] << " ";
		res[i] = 0;
	}
	cout << endl;
}

int32_t main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int t = 1;
	cin >> t;
	for(int i = 1 ; i <= t ; i++) executor(i);
}


/*input
6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3
*/

#include<bits/stdc++.h>
#define printarr(a,n) for(int i=0;i<n;i++) {cout << a[i] << " ";} cout << "\n"
#define getarr(a,n) for(int i=0;i<n;i++) cin >> a[i]
#define deb2d(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++) cout << setw(3) << a[i][j] << " "; cout << "\n";} cout << "\n\n";cin.ignore();
#define deb(x) cout << x << " ";
#define pb(x) push_back(x)
#define vi vector<int>
#define pii pair<int,int>
#define int long long int
#define endl "\n"
#define MOD 1000000007
#define bak cout << "Blah\n"
using namespace std;
const int MAX = (int) 1e5;
vi res(2*MAX + 5);

void dfs(int i, vi a, int n){
	vi visited(n);
	int cnt = 0;
	while(visited[i] == 0){
		visited[i] = 1;
		i = a[i];
		cnt++;
	}
	for(int i = 0; i < n; ++i){
		if(visited[i]) res[i] = cnt;
	}
}

void executor(int test_case){
	int n,k;
	cin >> n;
	vi a(n);
	//bak;
	//printarr(res,n);
	for(auto &itr:a){
		cin >> itr;
		--itr;
	}
	for(int i = 0; i < n; ++i){
		if(res[i] == 0){
			dfs(i,a,n);
		}
	}
	printarr(res,n);
	for(int i = 0; i < n; ++i){
		res[i] = 0;
	}
}

int32_t main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int t = 1;
	cin >> t;
	for(int i = 1 ; i <= t ; i++) executor(i);
}


/*input
6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3
*/

#include<bits/stdc++.h>
#define printarr(a,n) for(int i=0;i<n;i++) {cout << a[i] << " ";} cout << "\n"
#define getarr(a,n) for(int i=0;i<n;i++) cin >> a[i]
#define deb2d(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++) cout << setw(3) << a[i][j] << " "; cout << "\n";} cout << "\n\n";cin.ignore();
#define deb(x) cout << x << " ";
#define pb(x) push_back(x)
#define vi vector<int>
#define pii pair<int,int>
#define int long long int
#define endl "\n"
#define MOD 1000000007
#define bak cout << "Blah\n"
using namespace std;
const int MAX = (int) 1e5;
vi res(2*MAX + 5);
map<int,int> mp;
void dfs(int i, vi a, int n,int clr){
	vi visited(n);
	int cnt = 0;
	while(res[i] == 0){
		res[i] = clr;
		i = a[i];
		cnt++;
	}
	mp[clr] = cnt;
}

void executor(int test_case){
	mp.clear();
	int n,k;
	cin >> n;
	vi a(n);
	for(auto &itr:a){
		cin >> itr;
		--itr;
	}
	int clr = 1;
	for(int i = 0; i < n; ++i){
		if(res[i] == 0){
			dfs(i,a,n,clr);
			clr++;
		}
	}
	for(int i = 0; i < n; ++i){
		cout << mp[res[i]] << " ";
		res[i] = 0;
	}
	cout << endl;
}

int32_t main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int t = 1;
	cin >> t;
	for(int i = 1 ; i <= t ; i++) executor(i);
}


/*input
6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3
*/

#include<bits/stdc++.h>
#define printarr(a,n) for(int i=0;i<n;i++) {cout << a[i] << " ";} cout << "\n"
#define getarr(a,n) for(int i=0;i<n;i++) cin >> a[i]
#define deb2d(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++) cout << setw(3) << a[i][j] << " "; cout << "\n";} cout << "\n\n";cin.ignore();
#define deb(x) cout << x << " ";
#define pb(x) push_back(x)
#define vi vector<int>
#define pii pair<int,int>
#define endl "\n"
#define MOD 1000000007
#define bak cout << "Blah\n"
using namespace std;
const int MAX = (int) 1e5;

void executor(int test_case){
	int n,k;
	cin >> n;
	vi a(n);
	for(auto &itr:a){
		cin >> itr;
		--itr;
	}
	vi ans(n);
	vi visited(n);

	for(int i = 0; i < n; ++i){
		if(!visited[i]){
			vi temp;
			while(!visited[i]){
				temp.pb(i);
				visited[i] = 1;
				i = a[i];
			}
			for(int t: temp)
				ans[t] = temp.size();
		}
	}
	printarr(ans,n);
}

int32_t main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int t = 1;
	cin >> t;
	for(int i = 1 ; i <= t ; i++) executor(i);
}


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

vector<int> ans(n);

void dfs(int start, vector<int> per, vector<bool> &vis, int &len) {
	vis[start] = true;
	if(!vis[per[start]-1]) dfs(per[start]-1, per, vis, ++len);
	ans[start] = len;
}

int main() {
	int q; cin >> q;
	while(q--) {
		int n; cin >> n;
		vector<int> per(n);
		vector<bool> vis(n, false);

		for(int i = 0; i < n; i++) cin >> per[i];
		for(int i = 0; i < n; i++) {
			int len = 1;
			if(!vis[i]) dfs(i, per, vis, len);
		}

		for(int i: ans) cout << i << " ";
		cout << endl;
	}
	return 0;
}


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

int main() {
	int q; cin >> q;
	while(q--) {
		int n; cin >> n;
		int per[n];
		int ans[n];
		memset(ans, -1, sizeof ans);

		for(int i = 0; i < n; i++) cin >> per[i];
		for(int i = 0; i < n; i++) {
			int len = 1;
			if(ans[i] == -1) {
				int j = i;
				while(i != per[j] - 1) len++, j = per[j]-1;
				j = i, ans[j] = len;
				while(i != per[j] - 1) {
					j = per[j] - 1;
					ans[j] = len;
				}
			}
		}

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


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

void dfs(int start, vector<int> per, vector<bool> &vis, vector<int> &ans, int &len) {
	vis[start] = true;
	if(!vis[per[start]-1]) dfs(per[start]-1, per, vis, ans, ++len);
	ans[start] = len;
}

int main() {
	int q; cin >> q;
	while(q--) {
		int n; cin >> n;
		vector<int> per(n);
		vector<int> ans(n);
		vector<bool> vis(n, false);

		for(int i = 0; i < n; i++) cin >> per[i];
		for(int i = 0; i < n; i++) {
			int len = 1;
			if(!vis[i]) dfs(i, per, vis, ans, len);
		}

		for(int i: ans) cout << i << " ";
		cout << endl;
	}
	return 0;
}


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

void dfs(int start, vector<int> per, int loop_start, vector<int> &ans, int &len) {
	if(per[start]-1 != loop_start) dfs(per[start]-1, per, loop_start, ans, ++len);
	ans[start] = len;
}

int main() {
	int q; cin >> q;
	while(q--) {
		int n; cin >> n;
		vector<int> per(n);
		vector<int> ans(n, -1);

		for(int i = 0; i < n; i++) cin >> per[i];
		for(int i = 0; i < n; i++) {
			int len = 1;
			if(ans[i] == -1) dfs(i, per, i, ans, len);
		}

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


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

vector<int> ans(200005);

void dfs(int start, vector<int> per, vector<bool> &vis, int &len) {
	vis[start] = true;
	if(!vis[per[start]-1]) dfs(per[start]-1, per, vis, ++len);
	ans[start] = len;
}

int main() {
	int q; cin >> q;
	while(q--) {
		int n; cin >> n;
		vector<int> per(n);
		vector<bool> vis(n, false);

		for(int i = 0; i < n; i++) cin >> per[i];
		for(int i = 0; i < n; i++) {
			int len = 1;
			if(!vis[i]) dfs(i, per, vis, len);
		}

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


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

vector<int> ans(200005);

void dfs(int start, vector<int> per, vector<bool> &vis, int &len) {
	vis[start] = true;
	if(!vis[per[start]-1]) dfs(per[start]-1, per, vis, ++len);
	ans[start] = len;
}

int main() {
	int q; cin >> q;
	while(q--) {
		int n; cin >> n;
		vector<int> per(n);
		vector<bool> vis(n, false);

		for(int i = 0; i < n; i++) cin >> per[i];
		for(int i = 0; i < n; i++) {
			int len = 1;
			if(!vis[i]) dfs(i, per, vis, len);
		}

		for(int i: ans) cout << i << " ";
		cout << endl;
	}
	return 0;
}


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

void dfs(int start, vector<int> per, vector<bool> &vis, int &len) {
	vis[start] = true;
	if(!vis[per[start]-1]) dfs(per[start]-1, per, vis, ++len);
	ans[start] = len;
}

int main() {
	int q; cin >> q;
	while(q--) {
		int n; cin >> n;
		vector<int> per(n);
		vector<bool> vis(n, false);
vector<int> ans(n);

		for(int i = 0; i < n; i++) cin >> per[i];
		for(int i = 0; i < n; i++) {
			int len = 1;
			if(!vis[i]) dfs(i, per, vis, len);
		}

		for(int i: ans) cout << i << " ";
		cout << endl;
	}
	return 0;
}


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

void dfs(int start, vector<int> per, vector<bool> &vis, vector<int> &ans, int &len) {
	vis[start] = true;
	if(!vis[per[start]-1]) dfs(per[start]-1, per, vis, ans, ++len);
	ans[start] = len;
}

int main() {
	int q; cin >> q;
	while(q--) {
		int n; cin >> n;
		vector<int> per(n);

		vector<int> ans(n);
		vector<bool> vis(n, false);

		for(int i = 0; i < n; i++) cin >> per[i];
		for(int i = 0; i < n; i++) {
			int len = 1;
			if(!vis[i]) dfs(i, per, vis, ans, len);
		}

		for(int i: ans) cout << i << " ";
		cout << endl;
	}
	return 0;
}


//#include<bits/stdc++.h>
//mama,Kissu pari na🥵
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<set>
#include<list>
#include<map>
#include<iterator>
#include<algorithm>
#include<utility>
#include<functional>
#include<numeric>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define nl "\n"
#define vd " "
#define fo(a,b,c) for(i=a;i<=b;i+=c)
#define Fo(a,b,c) for(i=a;i>=b;i-=c)
#define pb push_back
#define mp make_pair
#define pf pop_front
#define ppb pop_back
#define eb emplace_back
#define ins insert
#define all(v)  v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define tc(T) while(T--)
#define vec vector
#define co cout
#define in(a,n) fo(0,n-1,1){cin>>a[i];}
#define vin(a,n) fo(0,n-1,1){cin>>q;a.pb(q);}
#define out(a,n) fo(0,n-1,1){cout<<a[i]<<vd;}
#define MX(a) *max_element(a,a+n);
#define MN(a) *min_element(a,a+n);
ll T=1,inf=10000000000;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> T;
    tc(T)
    {
        ll n,sz,ans=0,u,mn,ev=0,mx,mini,i,v,q,p,j,k,w,h,l,m,x,y,od=0,total=0,cc=0,pp=0,flag=0,klag=0,sum=0;
        string s,s3,s1="YES",s2="NO";
        bool ok=1;
        ll on=0,tw=0;
        cin>>n;
        vector<ll> a,b;
        fo(0,n-1,1) {
            cin>>q;
            a.pb(q);
        }
        for(i=0; i<n; i++) {
                x=a[i];
                cc=1;
                v=i+1;
                while(x!=v) {
                    x=a[x-1];
                    cc++;
                }
                b.pb(cc);

        }
        out(b,n);

    }
    return 0;
}


//#include<bits/stdc++.h>
//mama,Kissu pari na🥵
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<set>
#include<list>
#include<map>
#include<iterator>
#include<algorithm>
#include<utility>
#include<functional>
#include<numeric>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define nl "\n"
#define vd " "
#define fo(a,b,c) for(i=a;i<=b;i+=c)
#define Fo(a,b,c) for(i=a;i>=b;i-=c)
#define pb push_back
#define mp make_pair
#define pf pop_front
#define ppb pop_back
#define eb emplace_back
#define ins insert
#define all(v)  v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define tc(T) while(T--)
#define vec vector
#define co cout
#define in(a,n) fo(0,n-1,1){cin>>a[i];}
#define vin(a,n) fo(0,n-1,1){cin>>q;a.pb(q);}
#define out(a,n) fo(0,n-1,1){cout<<a[i]<<vd;}
#define MX(a) *max_element(a,a+n);
#define MN(a) *min_element(a,a+n);
ll T=1,inf=10000000000;
int main()
{
    //ios::sync_with_stdio(0);
   // cin.tie(0);
    cin >> T;
    tc(T)
    {
        ll n,sz,ans=0,u,mn,ev=0,mx,mini,i,v,q,p,j,k,w,h,l,m,x,y,od=0,total=0,cc=0,pp=0,flag=0,klag=0,sum=0;
        string s,s3,s1="YES",s2="NO";
        bool ok=1;
        ll on=0,tw=0;
        cin>>n;
        vector<ll> a,c;
        ll b[n-1];
        map<ll,ll> cnt;
        fo(0,n-1,1) {
            cin>>q;
            a.pb(q);
            cnt[i]=0;

        }
        for(i=0; i<n; i++) {
            if(cnt[i]==0) {
                x=a[i];
                cc=1;
                v=i+1;
                if(x==v) {
                    b[i]=1;
                }
                else {
                    while(x!=v) {
                        x=a[x-1];
                        c.pb(x-1);
                        cc++;
                    }
                    for(j=0; j<c.size(); j++) {
                        cnt[c[j]]=cc;
                        b[c[j]]=cc;
                    }
                    c.clear();

                }

            }

        }
        out(b,n);
        co<<nl;

    }
    return 0;
}


//#include<bits/stdc++.h>
//mama,Kissu pari na🥵
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<set>
#include<list>
#include<map>
#include<iterator>
#include<algorithm>
#include<utility>
#include<functional>
#include<numeric>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define nl "\n"
#define vd " "
#define fo(a,b,c) for(i=a;i<=b;i+=c)
#define Fo(a,b,c) for(i=a;i>=b;i-=c)
#define pb push_back
#define mp make_pair
#define pf pop_front
#define ppb pop_back
#define eb emplace_back
#define ins insert
#define all(v)  v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define tc(T) while(T--)
#define vec vector
#define co cout
#define in(a,n) fo(0,n-1,1){cin>>a[i];}
#define vin(a,n) fo(0,n-1,1){cin>>q;a.pb(q);}
#define out(a,n) fo(0,n-1,1){cout<<a[i]<<vd;}
#define MX(a) *max_element(a,a+n);
#define MN(a) *min_element(a,a+n);
ll T=1,inf=10000000000;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> T;
    tc(T)
    {
        ll n,sz,ans=0,u,mn,ev=0,mx,mini,i,v,q,p,j,k,w,h,l,m,x,y,od=0,total=0,cc=0,pp=0,flag=0,klag=0,sum=0;
        string s,s3,s1="YES",s2="NO";
        bool ok=1;
        ll on=0,tw=0;
        cin>>n;
        vector<ll> a,c;
        ll b[n-1];
        map<ll,ll> cnt;
        fo(0,n-1,1) {
            cin>>q;
            a.pb(q);
            cnt[i]=0;

        }
        for(i=0; i<n; i++) {
            if(cnt[i]==0) {
                x=a[i];
                cc=1;
                v=i+1;
                if(x==v) {
                    b[i]=1;
                }
                else {
                    while(x!=v) {
                        x=a[x-1];
                        c.pb(x-1);
                        cc++;
                    }
                    for(j=0; j<c.size(); j++) {
                        cnt[c[j]]=cc;
                        b[c[j]]=cc;
                    }
                    c.clear();

                }

            }

        }
        out(b,n);
        co<<nl;

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MAX_N 40005
#define inf 0x3f3f3f3f
#define LL long long
#define uLL unsigned long long
const LL mod = 1e9+7;
const LL INF = 1e18;
typedef pair<int, int>P;
const double eps = 1e-6;

int dp[1505][26][1505];
int pre[1505];
int main()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    memset(pre, 0, sizeof(pre));
    memset(dp, 0, sizeof(dp));
    for(int i=1; i<n; i++) {
        if(s[i]==s[i-1])
            pre[i] = pre[i-1];
        else
            pre[i] = i;
    }
    for(int i=0; i<n; i++) {
        for(int j=0; j<26; j++) {
            if(s[i]==('a'+j)) {
                dp[i][j][0] = 1;
                if(i>=1)
                    dp[i][j][0] += dp[i-1][j][0];
            }
            for(int k=1; k<=i+1; k++) {
                if(s[i]==('a'+j) && pre[i]-1>=0)
                    dp[i][j][k] = (i-pre[i]+1)+dp[pre[i]-1][j][k];
                else if(s[i]==('a'+j) && pre[i]-1<0)
                    dp[i][j][k] = i-pre[i]+1;
                else if(s[i]!=('a'+j) && i>=1)
                    dp[i][j][k] = dp[i-1][j][k-1] + 1;
                else if(s[i]!=('a'+j) && i<1)
                    dp[i][j][k] = 1;
            }
            for(int k=i+2; k<=n; k++) {
                dp[i][j][k] = i + 1;
            }
        }
    }
    int q;
    cin >> q;
    while(q--) {
        int m;
        char c;
        scanf("%d %c", &m, &c);
        //printf("%d %c\n", m, c);
        int mx = 0;
        for(int i=0; i<n; i++) {
            mx = max(mx, dp[i][c-'a'][m]);
        }
        printf("%d\n", mx);
    }
}
/*
10
abbaabbaaa
100
2 b
*/


#include<bits/stdc++.h>
using namespace std;
#define MAX_N 40005
#define inf 0x3f3f3f3f
#define LL long long
#define uLL unsigned long long
const LL mod = 1e9+7;
const LL INF = 1e18;
typedef pair<int, int>P;
const double eps = 1e-6;

int dp[1505][26][1505];
int pre[1505];
int main()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    memset(pre, 0, sizeof(pre));
    memset(dp, 0, sizeof(dp));
    for(int i=1; i<n; i++) {
        if(s[i]==s[i-1])
            pre[i] = pre[i-1];
        else
            pre[i] = i;
    }
    for(int i=0; i<n; i++) {
        for(int j=0; j<26; j++) {
            if(s[i]==('a'+j))
                dp[i][j][0] = 1;
            for(int k=1; k<=i+1; k++) {
                if(s[i]==('a'+j))
                    dp[i][j][k] = (i-pre[i]+1)+dp[pre[i]-1][j][k];
                else if(i>=1)
                    dp[i][j][k] = dp[i-1][j][k-1] + 1;
                else
                    dp[i][j][k] = 1;
            }
        }
    }
    //printf("%d--\n", dp[1]['o'-'a'][1]);
    int q;
    cin >> q;
    while(q--) {
        int m;
        char c;
        scanf("%d %c", &m, &c);
        int mx = 0;
        for(int i=0; i<n; i++) {
            mx = max(mx, dp[i][c-'a'][m]);
        }
        printf("%d\n", mx);
    }
}


#include<bits/stdc++.h>
using namespace std;
#define MAX_N 40005
#define inf 0x3f3f3f3f
#define LL long long
#define uLL unsigned long long
const LL mod = 1e9+7;
const LL INF = 1e18;
typedef pair<int, int>P;
const double eps = 1e-6;

int dp[26][1505];
int n, q;
string s;
void init()
{
    for(int i=0; i<26; i++) {
        for(int j=1; j<=n; j++) {
            char c = ('a'+i);
            int l = 0;
            int r = 0;
            int tmp = s[0]!=c;
            int ans = 0;
            while(r>=l && r<n) {
                if(j>=tmp) {
                    r++;
                    tmp += (s[r]!=c);
                }
                else {
                    tmp -= (s[l]!=c);
                    l++;
                }
                if(j>=tmp)
                    ans = max(ans, r-l+1);
            }
            dp[i][j] = ans;
        }
    }
}
int main()
{
    cin >> n;
    cin >> s;
    cin >> q;
    init();
    while(q--) {
        int m;
        char c;
        scanf("%d %c", &m ,&c);
        printf("%d\n", dp[c-'a'][m]);
    }
}


#include<bits/stdc++.h>
using namespace std;
#define MAX_N 40005
#define inf 0x3f3f3f3f
#define LL long long
#define uLL unsigned long long
const LL mod = 1e9+7;
const LL INF = 1e18;
typedef pair<int, int>P;
const double eps = 1e-6;

int dp[1505][26][1505];
int pre[1505];
int main()
{
    int n;
    scanf("%d", &n);
    char s[1505];
    scanf("%s", s);
    memset(pre, 0, sizeof(pre));
    memset(dp, 0, sizeof(dp));
    for(int i=1; i<n; i++) {
        if(s[i]==s[i-1])
            pre[i] = pre[i-1];
        else
            pre[i] = i;
    }
    for(int i=0; i<n; i++) {
        for(int j=0; j<26; j++) {
            if(s[i]==('a'+j)) {
                dp[i][j][0] = 1;
                if(i>=1)
                    dp[i][j][0] += dp[i-1][j][0];
            }
            for(int k=1; k<=i+1; k++) {
                if(s[i]==('a'+j)) {
                    if(pre[i]-1>=0)
                        dp[i][j][k] = (i-pre[i]+1)+dp[pre[i]-1][j][k];
                    else
                        dp[i][j][k] = i-pre[i]+1;
                }
                else {
                    if(i>=1)
                        dp[i][j][k] = dp[i-1][j][k-1] + 1;
                    else
                        dp[i][j][k] = 1;
                }
            }
            for(int k=i+2; k<=n; k++) {
                dp[i][j][k] = i + 1;
            }
        }
    }
    int q;
    scanf("%d", &q);
    while(q--) {
        int m;
        char c;
        scanf("%d %c", &m, &c);
        //printf("%d %c\n", m, c);
        int mx = 0;
        for(int i=0; i<n; i++) {
            mx = max(mx, dp[i][c-'a'][m]);
        }
        printf("%d\n", mx);
    }
}
/*
10
abbaabbaaa
100
2 b
*/


#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include<stdio.h>
#include<string.h>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long

const int maxn=1000000;
int n;
char s[3000];
bool cmp(const int &e1,const int &e2){
    return e1>e2;
}
int head[3000];
int tot;
void init(){
    for(int i=0;i<30;i++)head[i]=-1;
    tot=0;
}
struct Edge{
    int to,next,w;
}e[maxn];
void addedge(int from,int to,int w){
    e[tot].to=to;e[tot].next=head[from];e[tot].w=w;head[from]=tot++;
}
int que[100000];
int main() {
    if(fopen("H:\\acm.txt", "r")!=NULL){
     freopen("H:\\acm.txt","r",stdin);
     }

    while(scanf("%d",&n)!=EOF){

        int vis[50];memset(vis,0,sizeof(vis));
        scanf("%s",s+1);
        init();
        for(int i=0;i<26;i++){
            char x=i+'a';
            for(int j=1;j<=n;j++){
                    int sb=j;
                if(s[j]=='a'+i){
                    int w=1;
                    while(j+1<=n&&s[j+1]=='a'+i){
                        w++; j++;
                    }
                    addedge(i,sb,w);
                }
            }
        }
        for(int i=0;i<26;i++){

            int u=head[i]; head[i]=-1;
            for(;u!=-1;u=e[u].next){
                addedge(i,e[u].to,e[u].w);
            }
        }
        int qn,q;char g[10];
        cin>>qn;
        for(int ii=1;ii<=qn;ii++){

            cin>>q>>g;
            int stq=q;
            int st=0,ed=0;
            int now,res=0;
             int u=g[0]-'a';
            if(head[u]==-1){
                printf("%d\n",min(q,n) );continue;
            }
            int final_res=0;

            int ei=head[u];
            res+=e[ei].w;
            int k=e[ei].next;
            que[ed++]=ei;
            final_res=max(final_res,res);
            final_res=max(final_res,min(res+q,n));
            for( ;k!=-1;k=e[k].next ){

                 int len=e[k].to-(e[que[ed-1]].to+e[que[ed-1]].w );
                 if(len<=q){
                    q-=len;
                      res+=len+e[k].w;
                         que[ed++]=k;
                        final_res=max(final_res,res);
                        final_res=max(final_res,min(res+q,n));
                        continue;

                 }

                 while(ed-st>=2){
                      int tlen=e[que[st+1]].to-(e[que[st]].to+e[que[st]].w) ;
                      q+=tlen;

                      res-=tlen+e[que[st]].w;
                        st++;
                      if(len<=q){
                        break;
                      }
                 }

                 if(len<=q){
                        q-=len;
                        res+=len+e[k].w;
                        que[ed++]=k;
                        final_res=max(final_res,res);
                        final_res=max(final_res,min(res+q,n) );
                        continue;
                 }else{
                        final_res=max(final_res,min(res+q,n) );
                        res=0;st=0,ed=0;q=stq;
                            res+=e[k].w;
                            que[ed++]=k;
                            final_res=max(final_res,res);
                            final_res=max(final_res,min(res+q,n) );
                 }
            }

                if(q>0){
                    final_res=max(final_res,min(res+q,n) );
                }
                printf("%d\n",final_res);
        }
    }

    return 0;
}


#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include<stdio.h>
#include<string.h>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long

const int maxn=1000000;
int n;
char s[3000];
bool cmp(const int &e1,const int &e2){
    return e1>e2;
}
int head[3000];
int tot;
void init(){
    for(int i=0;i<30;i++)head[i]=-1;
    tot=0;
}
struct Edge{
    int to,next,w;
}e[maxn];
void addedge(int from,int to,int w){
    e[tot].to=to;e[tot].next=head[from];e[tot].w=w;head[from]=tot++;

}
int que[100000];
int main() {
    if(fopen("H:\\acm.txt", "r")!=NULL){
     freopen("H:\\acm.txt","r",stdin);
     }

    while(scanf("%d",&n)!=EOF){

        int vis[50];memset(vis,0,sizeof(vis));
        scanf("%s",s+1);
        init();
        for(int i=0;i<26;i++){
            char x=i+'a';
            for(int j=1;j<=n;j++){
                    int sb=j;
                if(s[j]=='a'+i){
                        int w=1;
                    while(j+1<=n&&s[j+1]=='a'+i){
                        w++; j++;
                    }
                    addedge(i,sb,w);
                }
            }
        }
        for(int i=0;i<26;i++){

            int u=head[i]; head[i]=-1;
            for(;u!=-1;u=e[u].next){
                addedge(i,e[u].to,e[u].w);
            }
        }
        int qn,q;char g[10];
        cin>>qn;
        for(int ii=1;ii<=qn;ii++){

            cin>>q>>g;
            int stq=q;
            int st=0,ed=0;
            int now,res=0;
             int u=g[0]-'a';
            if(head[u]==-1){
                printf("%d\n",min(q,n) );continue;
            }
            int final_res=0;

            int ei=head[u];
            res+=e[ei].w;
            int k=e[ei].next;
            que[ed++]=ei;
            final_res=max(final_res,res);
            final_res=max(final_res,min(res+q,n));
            for( ;k!=-1;k=e[k].next ){

                 int len=e[k].to-(e[que[ed-1]].to+e[que[ed-1]].w );
                 if(len<=q){
                    q-=len;
                      res+=len+e[k].w;
                         que[ed++]=k;
                        final_res=max(final_res,res);
                        final_res=max(final_res,min(res+q,n));
                        continue;

                 }

                 while(ed-st>=2){
                      int tlen=e[que[st+1]].to-(e[que[st]].to+e[que[st]].w) ;
                      q+=tlen;
                      st++;
                      res-=tlen+e[que[st]].w;
                      if(len<=q){
                        break;
                      }
                 }

                 if(len<=q){
                        q-=len;
                        res+=len+e[k].w;
                        que[ed++]=k;
                        final_res=max(final_res,res);
                        continue;
                 }else{
                        final_res=max(final_res,min(res+q,n) );
                        res=0;st=0,ed=0;q=stq;
                            res+=e[k].w;
                            que[ed++]=k;
                            final_res=max(final_res,res);
                            final_res=max(final_res,min(res+q,n) );
                 }
            }

                if(q>0){
                    final_res=max(final_res,min(res+q,n) );
                }
                printf("%d\n",final_res);
        }
    }

    return 0;
}


    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn = 1505;
    char str[maxn];
    int n, dp[30][maxn], sum[30][maxn];

    void solve()
    {
        for(int i = 0; i < 26; i++)
        {
            for(int j = 0; j <= n; j++)
            {
                int l = 1, r = 1;
                int tmp = 0;
                while(r <= n)
                {
                    while(r-l+1 - (sum[i][r]-sum[i][l-1]) <= j) r++;
                    tmp = max(tmp, r-l);
                    l++;
                }
                dp[i][j] = tmp;
            }
        }
    }
    int main(void)
    {
        while(cin >> n)
        {
            memset(sum, 0, sizeof(sum));
            memset(dp, 0, sizeof(dp));
            for(int i = 1; i <= n; i++)
            {
                scanf(" %c", &str[i]);
                for(int j = 0; j < 26; j++)
                    sum[j][i] = sum[j][i-1];
                sum[str[i]-'a'][i]++;
            }
            solve();
            int q;
            scanf("%d", &q);
            while(q--)
            {
                int c;
                char ch;
                scanf("%d %c", &c, &ch);
                printf("%d\n", dp[ch-'a'][c]);
            }
        }
        return 0;
    }