#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<map>
#include<queue>
#include<set>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define fe(x) for(int i=be[x];i;i=e[i].ne)
using namespace std;
int n,xx,yy,et,be[210000],a[210000],f[210000],dt,di[65],g[65];//g[200200][65];
struct edg{
	int y,ne;
};
edg e[410000];
inline void add_edge(int x,int y){
	e[++et].y=y;
	e[et].ne=be[x];
	be[x]=et;
}
int gcd(int a,int b){
	if (b==0) return a;else return gcd(b,a%b);
}
void dfs0(int x,int fa){
	fe(x){
		int &y=e[i].y;
		if (y!=fa){
			f[y]=gcd(f[x],a[y]);
			dfs0(y,x);
		}
	}
}
void dfs(int x,int fa){
	fd(i,dt,1) if (g[i]<=1){
		if (di[i]>f[x]) f[x]=di[i];
		break;
	}
	fe(x){
		int &y=e[i].y;
		if (y!=fa){
			fo(j,1,dt) g[j]+=((a[y]%di[j])!=0);//fo(i,1,dt) g[i]+=((a[y]%di[i])!=0);//(a[y]%di[i]);//a[x]!!!
			dfs(y,x);
			fo(j,1,dt) g[j]-=((a[y]%di[j])!=0);	//fo(i,1,dt) g[i]-=((a[y]%di[i])!=0);//(a[y]%di[i]);//a[x]!!!
		}
	}
}
int main(){
	scanf("%d",&n);
	fo(i,1,n) scanf("%d",&a[i]);
	fo(i,2,n){
		scanf("%d%d",&xx,&yy);
		add_edge(xx,yy);
		add_edge(yy,xx);
	}
	f[1]=a[1];
	fe(1){
		f[e[i].y]=a[e[i].y];
		dfs0(e[i].y,1);//dfs0(e[i].y,-1);
	}
	fo(i,2,a[1]) if (a[1]%i==0) di[++dt]=i;
	dfs(1,-1);
	fo(i,1,n) printf("%d ",f[i]);
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<map>
#include<queue>
#include<set>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define fe(x) for(int i=be[x];i;i=e[i].ne)
using namespace std;
int n,xx,yy,et,be[210000],a[210000],f[210000];
struct edg{
	int y,ne;
};
edg e[410000];
inline void add_edge(int x,int y){
	e[++et].y=y;
	e[et].ne=be[x];
	be[x]=et;
}
int gcd(int a,int b){
	if (b==0) return a;else return gcd(b,a%b);
}
void dfs0(int x,int fa){
	fe(x){
		int &y=e[i].y;
		if (y!=fa){
			f[y]=gcd(f[x],a[y]);
			dfs0(y,x);
		}
	}
}
void dfs(int x,int fa,int d,int ci){
	if (ci>1) return;
	if (d>f[x]) f[x]=d;
	fe(x){
		int &y=e[i].y;
		if (y!=fa) dfs(y,x,d,ci+(a[y]%d!=0));
	}
}
int main(){
	scanf("%d",&n);
	fo(i,1,n) scanf("%d",&a[i]);
	fo(i,2,n){
		scanf("%d%d",&xx,&yy);
		add_edge(xx,yy);
		add_edge(yy,xx);
	}
	f[1]=a[1];
	fe(1){
		f[e[i].y]=a[e[i].y];
		dfs0(e[i].y,-1);
	}
	fo(d,2,a[1]) if (a[1]%d==0) dfs(1,-1,d,0);
	fo(i,1,n) printf("%d ",f[i]);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define mp make_pair
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define endl '\n'
#define pii pair<int ,int >
#define all(v) v.begin(),v.end()
#define test(t) int t;cin>>t;while(t--)
ll poww(ll x,ll y) {ll res=1;x%=mod; assert(y>=0); for(;y;y>>=1){if(y&1)res=res*x%mod;x=x*x%mod;}return res;}
using namespace std;

vector<int> v[200005];
int ans[200005],cgcd[200005];
bool vis[200005];
int p[200005],a[200005];

void dfs(int x){

    int i;
    if(x!=1){
        if(cgcd[p[x]]==ans[p[x]]){
            if(p[x]==1)
                ans[x]=max(a[x],max(ans[p[x]],__gcd(ans[p[x]],a[x])));
            else
            ans[x]=max(ans[p[x]],__gcd(ans[p[x]],a[x]));

        }
        else
            ans[x]=max(__gcd(ans[p[x]],a[x]),cgcd[p[x]]);

        cgcd[x]=__gcd(cgcd[p[x]],a[x]);

    }

    for(i=0;i<v[x].size();++i)
        if(!vis[v[x][i]]){
            vis[v[x][i]]=1;
            p[v[x][i]]=x;
            dfs(v[x][i]);
        }
}

int main()
{
    fast;
    //freopen("input.txt", "r+", stdin);
    //freopen("output.txt", "w+", stdout);

    int i,j,k,n;
    cin>>n;
    for(i=1;i<=n;++i)
        cin>>a[i];
    for(i=1;i<n;++i){
        cin>>j>>k;
        v[j].pb(k);
        v[k].pb(j);
    }
    vis[1]=true;
    ans[1]=cgcd[1]=a[1];
    dfs(1);
    for(i=1;i<=n;++i)
        cout<<ans[i]<<" ";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define mp make_pair
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define endl '\n'
#define pii pair<int ,int >
#define all(v) v.begin(),v.end()
#define test(t) int t;cin>>t;while(t--)
ll poww(ll x,ll y) {ll res=1;x%=mod; assert(y>=0); for(;y;y>>=1){if(y&1)res=res*x%mod;x=x*x%mod;}return res;}
using namespace std;

vector<int> v[200005];
int ans[200005];
bool vis[200005];
int d[200005],a[200005],p[200005];

void dfs(int x,int lvl,int gcd){

	    int i;
	   vector<int> div;

	    div.pb(1);
	    d[1]++;
	    if(a[x]==1)
            goto lb;
	    d[a[x]]++;
	    div.pb(a[x]);
	    if(x==1)
	    goto lb;

	    for(i=2;i*i<=a[x];++i)
	    		if(a[x]%i==0){

	    			d[i]++;
				div.pb(i);
	    			if(i*i!=a[x]){
	    				div.pb(a[x]/i);
	    				d[a[x]/i]++;
	    			}
//6 2 1 2

	    		}
	   sort(all(div));
	   for(i=div.size()-1;i>=0;--i)
	   	if(d[div[i]]>=lvl){
	   		ans[x]=div[i];
	   		break;
	   	}

	    lb:
	   ans[x]=max(ans[x],gcd);

	    for(i=0;i<v[x].size();++i)
			 if(!vis[v[x][i]]){
			     vis[v[x][i]]=1;
			      p[v[x][i]]=x;
			     dfs(v[x][i],lvl+1,__gcd(a[x],gcd));
			 }
	   for(i=div.size()-1;i>=0;--i)
	    d[div[i]]--;
	   div.clear();

}

int main()
{
	    fast;
	    //freopen("input.txt", "r+", stdin);
	    //freopen("output.txt", "w+", stdout);

	    int i,j,k,n;
	   cin>>n;
	    for(i=1;i<=n;++i)
			 cin>>a[i];
	    for(i=1;i<n;++i){
			 cin>>j>>k;
			 v[j].pb(k);
			 v[k].pb(j);
	    }
	    vis[1]=true;
	    ans[1]=a[1];
	    dfs(1,0,a[1]);
	    for(i=1;i<=n;++i)
			 cout<<ans[i]<<" ";
	    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define mp make_pair
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define endl '\n'
#define pii pair<int ,int >
#define all(v) v.begin(),v.end()
#define test(t) int t;cin>>t;while(t--)
ll poww(ll x,ll y) {ll res=1;x%=mod; assert(y>=0); for(;y;y>>=1){if(y&1)res=res*x%mod;x=x*x%mod;}return res;}
using namespace std;

vector<int> v[200005];
int ans[200005];
bool vis[200005];
int d[200005],a[200005],p[200005];

void dfs(int x,int lvl,int gcd){

	    int i;
	   vector<int> div;

	    div.pb(1);
	    d[1]++;
	    if(a[x]==1)
            goto lb;
	    d[a[x]]++;
	    div.pb(a[x]);

	    for(i=2;i*i<=a[x];++i)
	    		if(a[x]%i==0){

	    			d[i]++;
				div.pb(i);
	    			if(i*i!=a[x]){
	    				div.pb(a[x]/i);
	    				d[a[x]/i]++;
	    			}
//6 2 1 2

	    		}
	   if(x==1)
	    goto lb;

	   sort(all(div));
	   for(i=div.size()-1;i>=0;--i)
	   	if(d[div[i]]>=lvl){
	   		ans[x]=div[i];
	   		break;
	   	}

	    lb:
	   ans[x]=max(ans[x],gcd);

	    for(i=0;i<v[x].size();++i)
			 if(!vis[v[x][i]]){
			     vis[v[x][i]]=1;
			      p[v[x][i]]=x;
			     dfs(v[x][i],lvl+1,__gcd(a[x],gcd));
			 }
	   for(i=div.size()-1;i>=0;--i)
	    d[div[i]]--;
	   div.clear();

}

int main()
{
	    fast;
	    //freopen("input.txt", "r+", stdin);
	    //freopen("output.txt", "w+", stdout);

	    int i,j,k,n;
	   cin>>n;
	    for(i=1;i<=n;++i)
			 cin>>a[i];
	    for(i=1;i<n;++i){
			 cin>>j>>k;
			 v[j].pb(k);
			 v[k].pb(j);
	    }
	    vis[1]=true;
	    ans[1]=a[1];
	    dfs(1,0,a[1]);
	    for(i=1;i<=n;++i)
			 cout<<ans[i]<<" ";
	    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define mp make_pair
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define endl '\n'
#define pii pair<int ,int >
#define all(v) v.begin(),v.end()
#define test(t) int t;cin>>t;while(t--)
ll poww(ll x,ll y) {ll res=1;x%=mod; assert(y>=0); for(;y;y>>=1){if(y&1)res=res*x%mod;x=x*x%mod;}return res;}
using namespace std;

vector<int> v[200005];
int ans[200005],cgcd[200005];
bool vis[200005];
int p[200005],a[200005];

void dfs(int x){

    int i,c,k;
    if(x!=1){
        if(cgcd[p[x]]==ans[p[x]])
            ans[x]=max(ans[p[x]],__gcd(ans[p[x]],a[x]));
        else
            ans[x]=max(__gcd(ans[p[x]],a[x]),cgcd[p[x]]);

        cgcd[x]=__gcd(cgcd[p[x]],a[x]);

    }

    for(i=0;i<v[x].size();++i)
        if(!vis[v[x][i]]){
            vis[v[x][i]]=1;
            p[v[x][i]]=x;
            dfs(v[x][i]);
        }
}

int main()
{
    fast;
    //freopen("input.txt", "r+", stdin);
    //freopen("output.txt", "w+", stdout);

    int i,j,k,n;
    cin>>n;
    for(i=1;i<=n;++i)
        cin>>a[i];
    for(i=1;i<n;++i){
        cin>>j>>k;
        v[j].pb(k);
        v[k].pb(j);
    }
    vis[1]=true;
    ans[1]=cgcd[1]=a[1];
    dfs(1);
    for(i=1;i<=n;++i)
        cout<<ans[i]<<" ";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define mp make_pair
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define endl '\n'
#define pii pair<int ,int >
#define all(v) v.begin(),v.end()
#define test(t) int t;cin>>t;while(t--)
ll poww(ll x,ll y) {ll res=1;x%=mod; assert(y>=0); for(;y;y>>=1){if(y&1)res=res*x%mod;x=x*x%mod;}return res;}
using namespace std;

vector<int> v[200005];
int ans[200005];
bool vis[200005];
int d[200005],a[200005],p[200005];

void dfs(int x,int lvl,int gcd){

	    int i;
	   vector<int> div;
	    if(x==1)
	    goto lb;

	    div.pb(1);
	    d[1]++;
	    d[a[x]]++;
	    div.pb(a[x]);
	    for(i=2;i*i<=a[x];++i)
	    		if(a[x]%i==0){

	    			d[i]++;
				div.pb(i);
	    			if(i*i!=a[x]){
	    				div.pb(a[x]/i);
	    				d[a[x]/i]++;
	    			}
//6 2 1 2

	    		}
	   sort(all(div));
	   for(i=div.size()-1;i>=0;--i)
	   	if(d[div[i]]>=lvl){
	   		ans[x]=div[i];
	   		break;
	   	}
	   	else{
	   		d[div[i]]--;
	   		div.pop_back();
	   	}
	   ans[x]=max(ans[x],gcd);
	   for(;i>=0;--i)
	   d[div[i]]--;
	   div.clear();
	    lb:
	    for(i=0;i<v[x].size();++i)
			 if(!vis[v[x][i]]){
			     vis[v[x][i]]=1;
			      p[v[x][i]]=x;
			     dfs(v[x][i],lvl+1,__gcd(a[x],gcd));
			 }
}

int main()
{
	    fast;
	    //freopen("input.txt", "r+", stdin);
	    //freopen("output.txt", "w+", stdout);

	    int i,j,k,n;
	   cin>>n;
	    for(i=1;i<=n;++i)
			 cin>>a[i];
	    for(i=1;i<n;++i){
			 cin>>j>>k;
			 v[j].pb(k);
			 v[k].pb(j);
	    }
	    vis[1]=true;
	    ans[1]=a[1];
	    dfs(1,0,a[1]);
	    for(i=1;i<=n;++i)
			 cout<<ans[i]<<" ";
	    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define mp make_pair
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define endl '\n'
#define pii pair<int ,int >
#define all(v) v.begin(),v.end()
#define test(t) int t;cin>>t;while(t--)
ll poww(ll x,ll y) {ll res=1;x%=mod; assert(y>=0); for(;y;y>>=1){if(y&1)res=res*x%mod;x=x*x%mod;}return res;}
using namespace std;

vector<int> v[200005];
int ans[200005];
bool vis[200005];
int d[200005],a[200005],p[200005];

void dfs(int x,int lvl,int gcd){

	    int i;
	   vector<int> div;
	    if(x==1)
	    goto lb;

	    div.pb(1);
	    d[1]++;
	    d[x]++;
	    div.pb(x);
	    for(i=2;i*i<=x;++i)
	    		if(x%i==0){

	    			d[i]++;
				div.pb(i);
	    			if(i*i!=x){
	    				div.pb(x/i);
	    				d[x/i]++;
	    			}
//6 2 1 2

	    		}
	   sort(all(div));
	   for(i=div.size()-1;i>=0;--i)
	   	if(d[div[i]]>=lvl){
	   		ans[x]=div[i];
	   		break;
	   	}
	   	else{
	   		d[div[i]]--;
	   		div.pop_back();
	   	}
	   ans[x]=max(ans[x],gcd);
	   for(;i>=0;--i)
	   d[div[i]]--;
	   div.clear();
	    lb:
	    for(i=0;i<v[x].size();++i)
			 if(!vis[v[x][i]]){
			     vis[v[x][i]]=1;
			      p[v[x][i]]=x;
			     dfs(v[x][i],lvl+1,__gcd(a[x],gcd));
			 }
}

int main()
{
	    fast;
	    //freopen("input.txt", "r+", stdin);
	    //freopen("output.txt", "w+", stdout);

	    int i,j,k,n;
	   cin>>n;
	    for(i=1;i<=n;++i)
			 cin>>a[i];
	    for(i=1;i<n;++i){
			 cin>>j>>k;
			 v[j].pb(k);
			 v[k].pb(j);
	    }
	    vis[1]=true;
	    ans[1]=a[1];
	    dfs(1,0,a[1]);
	    for(i=1;i<=n;++i)
			 cout<<ans[i]<<" ";
	    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define mp make_pair
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define endl '\n'
#define pii pair<int ,int >
#define all(v) v.begin(),v.end()
#define test(t) int t;cin>>t;while(t--)
ll poww(ll x,ll y) {ll res=1;x%=mod; assert(y>=0); for(;y;y>>=1){if(y&1)res=res*x%mod;x=x*x%mod;}return res;}
using namespace std;

vector<int> v[200005];
int ans[200005],cgcd[200005];
bool vis[200005];
int p[200005],a[200005];

void dfs(int x){

    int i;
    if(x!=1){
        if(cgcd[p[x]]==ans[p[x]]){
            if(p[x]==1)
                ans[x]=max(a[x],max(ans[p[x]],__gcd(ans[p[x]],a[x])));
            else
                ans[x]=max(__gcd(a[x],cgcd[p[p[x]]]),max(ans[p[x]],__gcd(ans[p[x]],a[x])));

        }
        else
            ans[x]=max(__gcd(ans[p[x]],a[x]),cgcd[p[x]]);

        cgcd[x]=__gcd(cgcd[p[x]],a[x]);

    }

    for(i=0;i<v[x].size();++i)
        if(!vis[v[x][i]]){
            vis[v[x][i]]=1;
            p[v[x][i]]=x;
            dfs(v[x][i]);
        }
}

int main()
{
    fast;
    //freopen("input.txt", "r+", stdin);
    //freopen("output.txt", "w+", stdout);

    int i,j,k,n;
    cin>>n;
    for(i=1;i<=n;++i)
        cin>>a[i];
    for(i=1;i<n;++i){
        cin>>j>>k;
        v[j].pb(k);
        v[k].pb(j);
    }
    vis[1]=true;
    ans[1]=cgcd[1]=a[1];
    dfs(1);
    for(i=1;i<=n;++i)
        cout<<ans[i]<<" ";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define mp make_pair
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define endl '\n'
#define pii pair<int ,int >
#define all(v) v.begin(),v.end()
#define test(t) int t;cin>>t;while(t--)
ll poww(ll x,ll y) {ll res=1;x%=mod; assert(y>=0); for(;y;y>>=1){if(y&1)res=res*x%mod;x=x*x%mod;}return res;}
using namespace std;

vector<int> v[200005];
int ans[200005];
bool vis[200005];
int d[200005],a[200005],p[200005];

void dfs(int x,int lvl,int gcd){

	    int i;
	   vector<int> div;
	    if(x==1)
	    goto lb;

	    div.pb(1);
	    d[1]++;
	    d[a[x]]++;
	    div.pb(a[x]);
	    for(i=2;i*i<=a[x];++i)
	    		if(a[x]%i==0){

	    			d[i]++;
				div.pb(i);
	    			if(i*i!=a[x]){
	    				div.pb(a[x]/i);
	    				d[a[x]/i]++;
	    			}
//6 2 1 2

	    		}
	   sort(all(div));
	   for(i=div.size()-1;i>=0;--i)
	   	if(d[div[i]]>=lvl){
	   		ans[x]=div[i];
	   		break;
	   	}

	   ans[x]=max(ans[x],gcd);
	    lb:
	    for(i=0;i<v[x].size();++i)
			 if(!vis[v[x][i]]){
			     vis[v[x][i]]=1;
			      p[v[x][i]]=x;
			     dfs(v[x][i],lvl+1,__gcd(a[x],gcd));
			 }
	   for(i=div.size()-1;i>=0;--i)
	    d[div[i]]--;
	   div.clear();

}

int main()
{
	    fast;
	    //freopen("input.txt", "r+", stdin);
	    //freopen("output.txt", "w+", stdout);

	    int i,j,k,n;
	   cin>>n;
	    for(i=1;i<=n;++i)
			 cin>>a[i];
	    for(i=1;i<n;++i){
			 cin>>j>>k;
			 v[j].pb(k);
			 v[k].pb(j);
	    }
	    vis[1]=true;
	    ans[1]=a[1];
	    dfs(1,0,a[1]);
	    for(i=1;i<=n;++i)
			 cout<<ans[i]<<" ";
	    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define mp make_pair
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define endl '\n'
#define pii pair<int ,int >
#define all(v) v.begin(),v.end()
#define test(t) int t;cin>>t;while(t--)
ll poww(ll x,ll y) {ll res=1;x%=mod; assert(y>=0); for(;y;y>>=1){if(y&1)res=res*x%mod;x=x*x%mod;}return res;}
using namespace std;

vector<int> v[200005];
int ans[200005],cgcd[200005];
bool vis[200005];
int p[200005],a[200005];

void dfs(int x){

    int i,c,k;
    if(x!=1){
        if(cgcd[p[x]]==ans[p[x]])
            ans[x]=max(ans[p[x]],__gcd(ans[p[x]],a[x]));
        else
            ans[x]=max(__gcd(ans[p[x]],a[i]),cgcd[p[x]]);

        cgcd[x]=__gcd(cgcd[p[x]],a[x]);

    }

    for(i=0;i<v[x].size();++i)
        if(!vis[v[x][i]]){
            vis[v[x][i]]=1;
            p[v[x][i]]=x;
            dfs(v[x][i]);
        }
}

int main()
{
    fast;
    //freopen("input.txt", "r+", stdin);
    //freopen("output.txt", "w+", stdout);

    int i,j,k,n;
    cin>>n;
    for(i=1;i<=n;++i)
        cin>>a[i];
    for(i=1;i<n;++i){
        cin>>j>>k;
        v[j].pb(k);
        v[k].pb(j);
    }
    vis[1]=true;
    ans[1]=cgcd[1]=a[1];
    dfs(1);
    for(i=1;i<=n;++i)
        cout<<ans[i]<<" ";
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define mp make_pair
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define endl '\n'
#define pii pair<int ,int >
#define all(v) v.begin(),v.end()
#define test(t) int t;cin>>t;while(t--)
ll poww(ll x,ll y) {ll res=1;x%=mod; assert(y>=0); for(;y;y>>=1){if(y&1)res=res*x%mod;x=x*x%mod;}return res;}
using namespace std;

vector<int> v[200005];
int ans[200005];
bool vis[200005];
int d[200005],a[200005],p[200005];

void dfs(int x,int lvl){

	    int i;
	   vector<int> div;
	    if(x==1)
	    goto lb;

	    div.pb(1);
	    d[1]++;
	    d[x]++;
	    div.pb(x);
	    for(i=2;i*i<=x;++i)
	    		if(x%i==0){

	    			d[i]++;
				div.pb(i);
	    			if(i*i!=x){
	    				div.pb(x/i);
	    				d[x/i]++;
	    			}
//6 2 1 2

	    		}
	   sort(all(div));
	   for(i=div.size()-1;i>=0;--i)
	   	if(d[div[i]]>=lvl){
	   		ans[x]=div[i];
	   		break;
	   	}
	   	else{
	   		d[div[i]]--;
	   		div.pop_back();
	   	}
	   ans[x]=max(ans[x],ans[p[x]]);
	   for(;i>=0;--i)
	   d[div[i]]--;
	   div.clear();
	    lb:
	    for(i=0;i<v[x].size();++i)
			 if(!vis[v[x][i]]){
			     vis[v[x][i]]=1;
			      p[v[x][i]]=x;
			     dfs(v[x][i],lvl+1);
			 }
}

int main()
{
	    fast;
	    //freopen("input.txt", "r+", stdin);
	    //freopen("output.txt", "w+", stdout);

	    int i,j,k,n;
	   cin>>n;
	    for(i=1;i<=n;++i)
			 cin>>a[i];
	    for(i=1;i<n;++i){
			 cin>>j>>k;
			 v[j].pb(k);
			 v[k].pb(j);
	    }
	    vis[1]=true;
	    ans[1]=a[1];
	    dfs(1,0);
	    for(i=1;i<=n;++i)
			 cout<<ans[i]<<" ";
	    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int MAX = 2e5+10;
int a[MAX],n;
vector<int>G[MAX];
set<int>sol[MAX];
void dfs(int node,int par,int prev_gcd){
	for(auto P:sol[par])
        sol[node].insert(__gcd(a[node],P));

    sol[node].insert(prev_gcd);
    prev_gcd = __gcd(a[node], prev_gcd);
//	sol[node].insert(prev_gcd = (__gcd(a[node],prev_gcd)));
	for(auto v:G[node])
        if(v!=par){
		    dfs(v,node,prev_gcd);
	    }
    }
int main(){
//	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1,u,v;i<n;i++){
		cin>>u>>v;
		G[u].pb(v);G[v].pb(u);
	}
	dfs(1,0,0);
	for(int i=1;i<=n;i++)
		cout<<*(--s[i].end())<<' ';
}


#include <bits/stdc++.h>

using namespace std;
const int N = 5 * 1e5 +5;

int Ans[N], A[N];
vector<int> T[N];
set<pair<pair<int,int>, bool>> dp;

void dfs(int cur, int p, int mx, bool v){
	if (dp.count({{cur, mx}, v})) return;

	if (v) Ans[cur] = max(__gcd(mx, A[cur]), Ans[cur]);
//	else if (mx == 0) Ans[cur] = max(Ans[cur], A[cur]);
	else Ans[cur] = max(Ans[cur], mx);

	for (auto x : T[cur]){
		if (x == p) continue;
		dfs(x, cur, __gcd(mx, A[cur]), v);
		if (!v){
			if (cur == 0) dfs(x, cur, A[x], 1);
			else dfs(x, cur, mx, 1);
		}
	}

	dp.insert({{cur, mx}, v});
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;

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

	for (int i = 0; i < n - 1; ++i){
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		T[a].push_back(b);
		T[b].push_back(a);
	}

	dfs(0, 0, A[0], 0);

	Ans[0] = A[0];
	// for (int x : T[0])
	// 	Ans[x] = max(A[0], A[x]);

	for (int i = 0; i < n; ++i)
		cout << Ans[i] << " ";
	cout << "\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int MAX = 2e5+10;
int a[MAX],n;
vector<int>G[MAX];
set<int>sol[MAX];
void dfs(int node,int par,int prev_gcd){
	for(auto P:sol[par])
        sol[node].insert(__gcd(a[node],P));

    sol[node].insert(prev_gcd);
    prev_gcd = __gcd(a[node], prev_gcd);
//	sol[node].insert(prev_gcd = (__gcd(a[node],prev_gcd)));
	for(auto v:G[node])
        if(v!=par){
		    dfs(v,node,prev_gcd);
	    }
    }
int main(){
//	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1,u,v;i<n;i++){
		cin>>u>>v;
		G[u].pb(v);G[v].pb(u);
	}
    sol[1].insert(a[1]);
	dfs(1,0,0);
	for(int i=1;i<=n;i++)
		cout<<*(--sol[i].end())<<' ';
}


#include <bits/stdc++.h>

using namespace std;
const int N = 5 * 1e5 +5;

int Ans[N], A[N];
vector<int> T[N];
set<pair<pair<int,int>, bool>> dp;

void dfs(int cur, int p, int mx, bool v){
	if (dp.count({{cur, mx}, v})) return;

	if (v) Ans[cur] = max(__gcd(mx, A[cur]), Ans[cur]);
//	else if (mx == 0) Ans[cur] = max(Ans[cur], A[cur]);
	else Ans[cur] = max(Ans[cur], mx);

	for (auto x : T[cur]){
		if (x == p) continue;
		dfs(x, cur, __gcd(mx, A[cur]), v);
		if (!v){
			if (cur == 0) dfs(x, cur, A[x], 1);
			else dfs(x, cur, mx, 1);
		}
	}

	dp.insert({{cur, mx}, v});
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;

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

	for (int i = 0; i < n - 1; ++i){
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		T[a].push_back(b);
		T[b].push_back(a);
	}

	dfs(0, 0, A[0], 0);

	Ans[0] = A[0];
	for (int x : T[0])
		Ans[x] = max(A[0], A[x]);

	for (int i = 0; i < n; ++i)
		cout << Ans[i] << " ";
	cout << "\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5+10;
int a[MAX],n;
vector<int>G[MAX];
set<int>sol[MAX];

int _gcd(int a, int b, int res){
    if(a == 0 || b == 0)
        return max(a,b);
    else if(a == b)
        return res * a;
    else if( a % 2 == 0 && b % 2 == 0)
        return _gcd( (int)(a / 2), (int)(b / 2), 2 * res );
    else if(a % 2 == 0)
        return _gcd( (int)(a / 2), b, res);
    else if(b % 2 == 0)
        return _gcd(a, (int)(b / 2), res);
    else if( a > b )
        return _gcd(a - b, b, res);
    else
        return _gcd(a, b - a, res);
}

void dfs(int node,int par,int prev_gcd){
	for(auto P:sol[par])
        sol[node].insert(_gcd(a[node],P, 1));

    sol[node].insert(prev_gcd);
    prev_gcd = _gcd(a[node], prev_gcd, 1);
//	sol[node].insert(prev_gcd = (__gcd(a[node],prev_gcd)));
	for(auto v:G[node])
    {
        if(v == par)
            continue;
	    dfs(v,node,prev_gcd);
	}
}

int main(){
//	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i=1;i<=n;i++)
        cin>>a[i];

	for(int i=1,u,v;i<n;i++){
		cin >> u >> v;
		G[u].push_back(v);
        G[v].push_back(u);
	}
    sol[1].insert(a[1]);
	dfs(1,0,0);
	for(int i=1;i<=n;i++)
		cout << *(sol[i].rbegin()) << ' ';
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int MAX = 2e5+10;
int a[MAX],n;
vector<int>G[MAX];
set<int>sol[MAX];
void dfs(int node,int par,int prev_gcd){
	for(auto P:sol[par])
        sol[node].insert(__gcd(a[node],P));

    sol[node].insert(prev_gcd);
    prev_gcd = __gcd(a[node], prev_gcd);
//	sol[node].insert(prev_gcd = (__gcd(a[node],prev_gcd)));
	for(auto v:G[node])
        if(v!=par){
		    dfs(v,node,prev_gcd);
	    }
    }
int main(){
//	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1,u,v;i<n;i++){
		cin>>u>>v;
		G[u].pb(v);G[v].pb(u);
	}
    sol[1].insert(a[1]);
	dfs(1,0,0);
	for(int i=1;i<=n;i++)
		cout<<*(sol[i].rbegin())<<' ';
}


#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5+10;
int a[MAX],n;
vector<int>G[MAX];
set<int>sol[MAX];
void dfs(int node,int par,int prev_gcd){
	for(auto P:sol[par])
        sol[node].insert(__gcd(a[node],P));

    sol[node].insert(prev_gcd);
    prev_gcd = __gcd(a[node], prev_gcd);
//	sol[node].insert(prev_gcd = (__gcd(a[node],prev_gcd)));
	for(auto v:G[node])
    {
        if(v == par)
            continue;
	    dfs(v,node,prev_gcd);
	}
}
int main(){
//	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i=1;i<=n;i++)
        cin>>a[i];

	for(int i=1,u,v;i<n;i++){
		cin >> u >> v;
		G[u].push_back(v);
        G[v].push_back(u);
	}
    sol[1].insert(a[1]);
	dfs(1,0,0);
	for(int i=1;i<=n;i++)
		cout << *(sol[i].rbegin()) << ' ';
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int MAX = 2e5+10;
int a[MAX],n;
vector<int>G[MAX];
set<int>sol[MAX];
void dfs(int node,int par,int prev_gcd){
	for(auto P:sol[par])
        sol[node].insert(__gcd(a[node],P));

    sol[node].insert(prev_gcd);
    prev_gcd = __gcd(a[node], prev_gcd);
//	sol[node].insert(prev_gcd = (__gcd(a[node],prev_gcd)));
	for(auto v:G[node])
        if(v!=par){
		    dfs(v,node,prev_gcd);
	    }
    }
int main(){
//	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1,u,v;i<n;i++){
		cin>>u>>v;
		G[u].pb(v);G[v].pb(u);
	}
	dfs(1,0,0);
	for(int i=1;i<=n;i++)
		cout<<*(--sol[i].end())<<' ';
}


#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5+10;
int a[MAX],n;
vector<int>G[MAX];
set<int>sol[MAX];
void dfs(int node,int par,int prev_gcd){
	for(auto P:sol[par])
        sol[node].insert(_gcd(a[node],P, 1));

    sol[node].insert(prev_gcd);
    prev_gcd = _gcd(a[node], prev_gcd, 1);
//	sol[node].insert(prev_gcd = (__gcd(a[node],prev_gcd)));
	for(auto v:G[node])
    {
        if(v == par)
            continue;
	    dfs(v,node,prev_gcd);
	}
}

int _gcd(int a, int b, int res){
    if(a == 0 || b == 0)
        return max(a,b);
    else if(a == b)
        return res * a;
    else if( a % 2 == 0 && b % 2 == 0)
        return _gcd( (int)(a / 2), (int)(b / 2), 2 * res );
    else if(a % 2 == 0)
        return _gcd( (int)(a / 2), b, res);
    else if(b % 2 == 0)
        return _gcd(a, (int)(b / 2), res);
    else if( a > b )
        return _gcd(a - b, b, res);
    else
        return _gcd(a, b - a, res);
}
int main(){
//	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i=1;i<=n;i++)
        cin>>a[i];

	for(int i=1,u,v;i<n;i++){
		cin >> u >> v;
		G[u].push_back(v);
        G[v].push_back(u);
	}
    sol[1].insert(a[1]);
	dfs(1,0,0);
	for(int i=1;i<=n;i++)
		cout << *(sol[i].rbegin()) << ' ';
}


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

#define fr freopen("in.txt", "r", stdin);
#define fw freopen("out.txt", "w", stdout);
#define sd(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define sf(x) scanf("%lf", &x);
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define mem(v, x) memset(v, x, sizeof(v));
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define N 2 * 100010

vector<int> F[N], g[N];
int res[N], cnt[N], arr[N];

inline void pre()
{
    int x, y;
    for (x = 1; x < N; x++)
        for (y = x; y < N; y += x)
            F[y].EB(x);

    mem(cnt, 0);
}

inline int gcd(int a, int b)
{
    while (b != 0)
    {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}

inline void rec(int c, int p, int l, int Gcd)
{
    res[c] = Gcd;
    for (auto it : F[arr[c]])
    {
        cnt[it]++;
        if (cnt[it] >= l - 1 || cnt[it] == l)
            res[c] = max(res[c], it);
    }

    for (auto it : g[c])
    {
        if (it != p)
        {
            rec(it, c, l + 1, gcd(Gcd, arr[c]));
        }
    }

    for (auto it : F[arr[c]])
        cnt[it]--;
}

int main()
{
    //fr;
    //fw;

    int n;
    sd(n);

    for (int x = 1; x <= n; x++)
        sd(arr[x]);

    for (int x = 1; x < n; x++)
    {
        int u, v;
        sd(u);
        sd(v);
        g[u].EB(v);
        g[v].EB(u);
    }

    pre();
    rec(1, 1, 1, 0);

    for (int x = 1; x <= n; x++)
        printf("%d ", res[x]);
}


//Tanuj Khattar
#include<bits/stdc++.h>

using namespace std;

typedef pair<int,int>   II;
typedef vector< II >      VII;
typedef vector<int>     VI;
typedef vector< VI > 	VVI;
typedef long long int 	LL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define si(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define sll(n) scanf("%lld",&n)
#define lldout(n) printf("%lld\n",n)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int N = int(2e5)+10;
int A[N],ans[N],cnt[N];
VI D[N],g[N];
void dfs(int x,int p,int l = 1, int g = 0){
  ans[x] = g;
  for(auto d : D[A[x]]){
    cnt[d]++; //cout<<d<<" ";
    if(cnt[d] == l)
      ans[x] = max(ans[x],d);
  }//cout<<"\n";
  for(auto y : ::g[x]){
    if(y == p)continue;
    dfs(y,x,l+1,__gcd(g,A[x]));
  }
  for(auto d : D[A[x]])
    cnt[d]--;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

  for(int i=1;i<N;i++)
    for(int j=i;j<N;j+=i)
      D[j].PB(i);
  int n;si(n);
  for(int i=1;i<=n;i++)
    si(A[i]);
  for(int i=1;i<n;i++){
    int x,y;
    si(x);si(y);
    g[x].PB(y);
    g[y].PB(x);
  }
  dfs(1,1);
  for(int i=1;i<=n;i++)
    printf("%d ",ans[i]);
  puts("");
	return 0;
}


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

#define fr freopen("in.txt", "r", stdin);
#define fw freopen("out.txt", "w", stdout);
#define sd(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define sf(x) scanf("%lf", &x);
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define mem(v, x) memset(v, x, sizeof(v));
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define N 2 * 100010

vector<int> F[N+5], g[N+5];
int res[N+5], cnt[N+5], arr[N+5];

inline void pre()
{
    int x, y;
    for (x = 1; x < N; x++)
        for (y = x; y < N; y += x)
            F[y].EB(x);

    mem(cnt, 0);
}

inline int gcd(int a, int b)
{
    while (b != 0)
    {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}

inline void rec(int c, int p, int l, int Gcd)
{
    res[c] = Gcd;
    for (auto it : F[arr[c]])
    {
        cnt[it]++;
        if (cnt[it] == l - 1 || cnt[it] == l)
            res[c] = max(res[c], it);
    }

    for (auto it : g[c])
    {
        if (it != p)
        {
            rec(it, c, l + 1, __gcd(Gcd, arr[c]));
        }
    }

    for (auto it : F[arr[c]])
        cnt[it]--;
}

int main()
{
    //fr;
    //fw;

    int n;
    sd(n);

    for (int x = 1; x <= n; x++)
        sd(arr[x]);

    for (int x = 1; x < n; x++)
    {
        int u, v;
        sd(u);
        sd(v);
        g[u].EB(v);
        g[v].EB(u);
    }

    pre();
    rec(1, 1, 0, 0);

    res[1] = arr[1];
    for (int x = 1; x <= n; x++)
        printf("%d ", res[x]);
}


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

#define fr freopen("in.txt", "r", stdin);
#define fw freopen("out.txt", "w", stdout);
#define sd(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define sf(x) scanf("%lf", &x);
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define mem(v, x) memset(v, x, sizeof(v));
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define N 2 * 100010

vector<int> F[N + 5], g[N + 5];
int res[N + 5], cnt[N + 5], arr[N + 5];

inline void pre()
{
    int x, y;
    for (x = 1; x < N; x++)
        for (y = x; y < N; y += x)
            F[y].EB(x);

    mem(cnt, 0);
}

inline int gcd(int a, int b)
{
    while (b != 0)
    {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}

inline void rec(int c, int p, int l, int Gcd)
{
    res[c] = Gcd;
    for (auto it : F[arr[c]])
    {
        cnt[it]++;
        if (cnt[it] >= l - 1) // || cnt[it] == l)
            res[c] = max(res[c], it);
    }

    for (auto it : g[c])
    {
        if (it != p)
        {
            rec(it, c, l + 1, __gcd(Gcd, arr[c]));
        }
    }

    for (auto it : F[arr[c]])
        cnt[it]--;
}

int main()
{
    //fr;
    //fw;

    int n;
    sd(n);

    for (int x = 1; x <= n; x++)
        sd(arr[x]);

    for (int x = 1; x < n; x++)
    {
        int u, v;
        sd(u);
        sd(v);
        g[u].EB(v);
        g[v].EB(u);
    }

    pre();
    rec(1, 1, 1, 0);

    //res[1] = arr[1];
    for (int x = 1; x <= n; x++)
        printf("%d ", res[x]);
}


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

#define fr freopen("in.txt", "r", stdin);
#define fw freopen("out.txt", "w", stdout);
#define sd(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define sf(x) scanf("%lf", &x);
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define mem(v, x) memset(v, x, sizeof(v));
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define N 2 * 100010

vector<int> F[N + 5], g[N + 5];
int res[N + 5], cnt[N + 5], arr[N + 5];

inline void pre()
{
    int x, y;
    for (x = 1; x < N; x++)
        for (y = x; y < N; y += x)
            F[y].EB(x);

    mem(cnt, 0);
}

inline int gcd(int a, int b)
{
    while (b != 0)
    {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}

inline void rec(int c, int p, int l, int Gcd)
{
    res[c] = Gcd;
    for (auto it : F[arr[c]])
    {
        cnt[it]++;
        if (cnt[it] >= l - 1 || cnt[it] == l)
            res[c] = max(res[c], it);
    }

    for (auto it : g[c])
    {
        if (it != p)
        {
            rec(it, c, l + 1, gcd(Gcd, arr[c]));
        }
    }

    for (auto it : F[arr[c]])
        cnt[it]--;
}

int main()
{
    //fr;
    //fw;

    int n;
    sd(n);

    for (int x = 1; x <= n; x++)
        sd(arr[x]);

    for (int x = 1; x < n; x++)
    {
        int u, v;
        sd(u);
        sd(v);
        g[u].EB(v);
        g[v].EB(u);
    }

    pre();
    rec(1, 1, 1, 0);

    //res[1] = arr[1];
    for (int x = 1; x <= n; x++)
        printf("%d ", res[x]);
}


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

#define fr freopen("in.txt", "r", stdin);
#define fw freopen("out.txt", "w", stdout);
#define sd(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define sf(x) scanf("%lf", &x);
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define mem(v, x) memset(v, x, sizeof(v));
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define N 3 * 100000

vector<int> F[N], g[N];
int res[N], cnt[N], arr[N];

inline void pre()
{
    int x, y;
    for (x = 1; x < N; x++)
        for (y = x; y < N; y += x)
            F[y].EB(x);

    mem(cnt, 0);
}

inline int gcd(int a, int b)
{
    while (b != 0)
    {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}

inline void rec(int c, int p, int l, int Gcd)
{
    res[c] = Gcd;
    for (auto it : F[arr[c]])
    {
        cnt[it]++;
        if (cnt[it] == l - 1 || cnt[it] == l)
            res[c] = max(res[c], it);
    }

    for (auto it : g[c])
    {
        if (it != p)
        {
            rec(it, c, l + 1, __gcd(Gcd, arr[c]));
        }
    }

    for (auto it : F[arr[c]])
        cnt[it]--;
}

int main()
{
    //fr;
    //fw;

    int n;
    sd(n);

    for (int x = 1; x <= n; x++)
        sd(arr[x]);

    for (int x = 1; x < n; x++)
    {
        int u, v;
        sd(u);
        sd(v);
        g[u].EB(v);
        g[v].EB(u);
    }

    pre();
    rec(1, 1, 0, 0);

    res[1] = arr[1];
    for (int x = 1; x <= n; x++)
        printf("%d ", res[x]);
}


//Tanuj Khattar
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> II;
typedef vector<II> VII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long int LL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(), a.end()
#define SET(a, b) memset(a, b, sizeof(a))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define si(n) scanf("%d", &n)
#define dout(n) printf("%d\n", n)
#define sll(n) scanf("%lld", &n)
#define lldout(n) printf("%lld\n", n)
#define fast_io                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1)
{
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args)
{
    const char *comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | ";
    __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

const int N = int(2e5) + 10;
int A[N], ans[N], cnt[N];
VI D[N], g[N];
void dfs(int x, int p, int l = 1, int g = 0)
{
    ans[x] = g;
    for (auto d : D[A[x]])
    {
        cnt[d]++; //cout<<d<<" ";
        if (cnt[d] == l - 1)
            ans[x] = max(ans[x], d);
    } //cout<<"\n";
    for (auto y : ::g[x])
    {
        if (y == p)
            continue;
        dfs(y, x, l + 1, __gcd(g, A[x]));
    }
    for (auto d : D[A[x]])
        cnt[d]--;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    for (int i = 1; i < N; i++)
        for (int j = i; j < N; j += i)
            D[j].PB(i);
    int n;
    si(n);
    for (int i = 1; i <= n; i++)
        si(A[i]);
    for (int i = 1; i < n; i++)
    {
        int x, y;
        si(x);
        si(y);
        g[x].PB(y);
        g[y].PB(x);
    }
    dfs(1, 1);
    for (int i = 1; i <= n; i++)
        printf("%d ", ans[i]);
    puts("");
    return 0;
}


//Tanuj Khattar
#include<bits/stdc++.h>

using namespace std;

typedef pair<int,int>   II;
typedef vector< II >      VII;
typedef vector<int>     VI;
typedef vector< VI > 	VVI;
typedef long long int 	LL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define si(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define sll(n) scanf("%lld",&n)
#define lldout(n) printf("%lld\n",n)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int N = int(2e5)+10;
int A[N],ans[N],cnt[N];
VI D[N],g[N];
void dfs(int x,int p,int l = 1, int g = 0){
  ans[x] = g;
  for(auto d : D[A[x]]){
    cnt[d]++; cout<<d<<" ";
    if(cnt[d] == l)
      ans[x] = max(ans[x],d);
  }cout<<"\n";
  for(auto y : ::g[x]){
    if(y == p)continue;
    dfs(y,x,l+1,__gcd(g,A[x]));
  }
  for(auto d : D[A[x]])
    cnt[d]--;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

  for(int i=1;i<N;i++)
    for(int j=i;j<N;j+=i)
      D[j].PB(i);
  int n;si(n);
  for(int i=1;i<=n;i++)
    si(A[i]);
  for(int i=1;i<n;i++){
    int x,y;
    si(x);si(y);
    g[x].PB(y);
    g[y].PB(x);
  }
  dfs(1,1);
  for(int i=1;i<=n;i++)
    printf("%d ",ans[i]);
  puts("");
	return 0;
}


//Tanuj Khattar
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> II;
typedef vector<II> VII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long int LL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(), a.end()
#define SET(a, b) memset(a, b, sizeof(a))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define si(n) scanf("%d", &n)
#define dout(n) printf("%d\n", n)
#define sll(n) scanf("%lld", &n)
#define lldout(n) printf("%lld\n", n)
#define fast_io                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1)
{
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args)
{
    const char *comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | ";
    __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

const int N = int(2e5) + 10;
int A[N], ans[N], cnt[N];
VI D[N], g[N];
void dfs(int x, int p, int l = 1, int g = 0)
{
    ans[x] = g;
    for (auto d : D[A[x]])
    {
        cnt[d]++; //cout<<d<<" ";
        if (cnt[d] == l - 1 || cnt[d] == l)
            ans[x] = max(ans[x], d);
    } //cout<<"\n";
    for (auto y : ::g[x])
    {
        if (y == p)
            continue;
        dfs(y, x, l + 1, __gcd(g, A[x]));
    }
    for (auto d : D[A[x]])
        cnt[d]--;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    for (int i = 1; i < N; i++)
        for (int j = i; j < N; j += i)
            D[j].PB(i);
    int n;
    si(n);
    for (int i = 1; i <= n; i++)
        si(A[i]);
    for (int i = 1; i < n; i++)
    {
        int x, y;
        si(x);
        si(y);
        g[x].PB(y);
        g[y].PB(x);
    }
    dfs(1, 1);
    for (int i = 1; i <= n; i++)
        printf("%d ", ans[i]);
    puts("");
    return 0;
}


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

#define fr freopen("in.txt", "r", stdin);
#define fw freopen("out.txt", "w", stdout);
#define sd(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define sf(x) scanf("%lf", &x);
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define mem(v, x) memset(v, x, sizeof(v));
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define N 2 * 100010

vector<int> F[N+5], g[N+5];
int res[N+5], cnt[N+5], arr[N+5];

inline void pre()
{
    int x, y;
    for (x = 1; x < N; x++)
        for (y = x; y < N; y += x)
            F[y].EB(x);

    mem(cnt, 0);
}

inline int gcd(int a, int b)
{
    while (b != 0)
    {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}

inline void rec(int c, int p, int l, int Gcd)
{
    res[c] = Gcd;
    for (auto it : F[arr[c]])
    {
        cnt[it]++;
        if (cnt[it] >= l - 1)// || cnt[it] == l)
            res[c] = max(res[c], it);
    }

    for (auto it : g[c])
    {
        if (it != p)
        {
            rec(it, c, l + 1, __gcd(Gcd, arr[c]));
        }
    }

    for (auto it : F[arr[c]])
        cnt[it]--;
}

int main()
{
    //fr;
    //fw;

    int n;
    sd(n);

    for (int x = 1; x <= n; x++)
        sd(arr[x]);

    for (int x = 1; x < n; x++)
    {
        int u, v;
        sd(u);
        sd(v);
        g[u].EB(v);
        g[v].EB(u);
    }

    pre();
    rec(1, 1, 0, 0);

    res[1] = arr[1];
    for (int x = 1; x <= n; x++)
        printf("%d ", res[x]);
}


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

#define fr freopen("in.txt", "r", stdin);
#define fw freopen("out.txt", "w", stdout);
#define sd(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define sf(x) scanf("%lf", &x);
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define mem(v, x) memset(v, x, sizeof(v));
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define N 3 * 100000

vector<int> F[N], g[N];
int res[N], cnt[N], arr[N];

inline void pre()
{
    int x, y;
    for (x = 1; x < N; x++)
        for (y = x; y < N; y += x)
            F[y].EB(x);

    mem(cnt, 0);
}

inline int gcd(int a, int b)
{
    while (b != 0)
    {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}

inline void rec(int c, int p, int l, int Gcd)
{
    res[c] = Gcd;
    for (auto it : F[arr[c]])
    {
        cnt[it]++;
        if (cnt[it] == l - 1 || cnt[it] == l)
            res[c] = max(res[c], it);
    }

    for (auto it : g[c])
    {
        if (it != p)
        {
            rec(it, c, l + 1, gcd(Gcd, arr[c]));
        }
    }

    for (auto it : F[arr[c]])
        cnt[it]--;
}

int main()
{
    //fr;
    //fw;

    int n;
    sd(n);

    for (int x = 1; x <= n; x++)
        sd(arr[x]);

    for (int x = 1; x < n; x++)
    {
        int u, v;
        sd(u);
        sd(v);
        g[u].EB(v);
        g[v].EB(u);
    }

    pre();
    rec(1, 1, 0, 0);

    res[1] = arr[1];
    for (int x = 1; x <= n; x++)
        printf("%d ", res[x]);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

const int maxn=2e5+5;
struct Edge{
	int to,next;
}edge[maxn];
int head[maxn],tot;
int a[maxn];

ll gcd(ll a,ll b){
	return b==0?a:gcd(b,a%b);
}

void addEdge(int u,int v){
	edge[tot].to=v;
	edge[tot].next=head[u];
	head[u]=tot++;
}

void dfs(int u,int pre){
	for(int i=head[u];i!=-1;i=edge[i].next){
		int v=edge[i].to;
		if(v==pre)continue;
		if(a[v]>a[u])a[v]=gcd(a[v],a[u]);
		else a[v]=a[u];
	}
}

int main(){
	int n;
	scanf("%d",&n);
	memset(head,-1,sizeof(head));
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=1;i<=n-1;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		addEdge(a,b);
		addEdge(b,a);
	}
	dfs(1,0);
	for(int i=1;i<=n;i++){
		printf("%d ",a[i]);
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

const int maxn=2e5+5;
struct Edge {
	int to,next;
} edge[maxn];
int head[maxn],tot;
int a[maxn];

ll gcd(ll a,ll b) {
	return b==0?a:gcd(b,a%b);
}

void addEdge(int u,int v) {
	edge[tot].to=v;
	edge[tot].next=head[u];
	head[u]=tot++;
}

void dfs(int u,int pre) {
	for(int i=head[u]; i!=-1; i=edge[i].next) {
		int v=edge[i].to;
		if(v==pre)continue;
		if(a[v]>a[u]) {

		} else a[v]=a[u];
	}
}

int main() {
	int n;
	scanf("%d",&n);
	memset(head,-1,sizeof(head));
	for(int i=1; i<=n; i++) {
		scanf("%d",&a[i]);
	}
	for(int i=1; i<=n-1; i++) {
		int a,b;
		scanf("%d%d",&a,&b);
		addEdge(a,b);
		addEdge(b,a);
	}
	dfs(1,0);
	for(int i=1; i<=n; i++) {
		printf("%d ",a[i]);
	}

	return 0;
}


///////////////////////////////////////////////////////////
//   All rights reserved.
//   Author: wayen
///////////////////////////////////////////////////////////

#define _CRT_SECURE_NO_WARNINGS 1
#include <functional>
#include <algorithm>
#include <exception>
#include <stdexcept>
#include <streambuf>
#include <iostream>
#include <typeinfo>
#include <valarray>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <complex>
#include <fstream>
#include <iomanip>
#include <istream>
#include <numeric>
#include <ostream>
#include <sstream>
#include <utility>
#include <cstdio>
#include <iosfwd>
#include <bitset>
#include <locale>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <ios>
#include <map>
#include <new>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef pair<int, int> pa;

int head[400005];
int nex[400005];
int to[400005];
int cnt = 0;
void add(int u, int v)
{
	nex[++cnt] = head[u];
	to[cnt] = v;
	head[u] = cnt;
}

int gcd(int a, int b)
{
	if (a < b)
		swap(a, b);
	return b ? gcd(b, a%b) : a;
}
int n;
int number[200005];
int vis[200005];
int set1To0[200005];
int set1To1[200005];
int ans[2000005];
int num[200005];//最多200005个因数
vector<int> q;

void dfs1(int now,int cnt)
{
	vis[now] = 1;
	for (int i = head[now]; i; i=nex[i])
	{
		int k = to[i];
		if (vis[k])
			continue;
		for (int j = 0; j < q.size(); j++)
		{
			if (number[k] % q[j] == 0)
				num[q[j]]++;
			if (num[q[j]] >= cnt)
				set1To1[k] = max(set1To1[k], q[j]);
		}
		dfs1(k, cnt + 1);
		for (int j = 0; j < q.size(); j++)
		{
			if (number[k] % q[j] == 0)
				num[q[j]]--;
		}
	}
}

void dfs2(int now)
{
	vis[now] = 1;
	for (int i = head[now]; i; i = nex[i])
	{
		int k = to[i];
		if (vis[k])
			continue;
		set1To0[k] = gcd(number[k], set1To0[now]);
		dfs2(k);
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> number[i + 1];
	}
	int a, b;
	for (int i = 1; i < n; i++)
	{
		cin >> a >> b;
		add(a, b);
		add(b, a);
	}
	memset(vis, 0, sizeof(vis));
	memset(num, 0, sizeof(num));
	for (int i = 1; i <= number[1]; i++)
	{
		if (number[1] % i == 0)
		{
			num[i]++;
			q.push_back(i);
		}
	}
	set1To1[1] = number[1];
	dfs1(1, 1);

	memset(vis, 0, sizeof(vis));
	set1To0[1] = 0;
	dfs2(1);
	int ans;
	for (int i = 0; i < n; i++)
	{
		ans = max(set1To0[i + 1], set1To1[i + 1]);

		cout << ans << " ";
	}
	return 0;
}

//        /＼7　　　 ∠＿/
//        / │　　 ／　／
//        │　Z ＿, ＜　／　　 / `ヽ
//        │　　　　　ヽ　　 / 〉
//        Y　　　　　`　 /　　/
//        ｲ●　､　●　　⊂⊃〈　　 /
//        ()　 へ　　　　 | ＼〈
//          > ｰ ､_　 ィ　 │ ／／
//        	/ へ　　 / ﾉ＜ | ＼＼
//        	ヽ_ﾉ　　(_／　 │／／
//        		7 | ／
//        		＞―r￣￣`ｰ―＿


#include<stdio.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
vector<int>edge[200010];
int a[200010],fa[200010],g[200010],d[1010],vis[200010];
int f(int x,int y)
{
	if(x<y){x=x+y;y=x-y;x=x-y;}
	if(x%y==0)return y;
	else return f(y,x%y);
}
void dfs(int x,int deep,vector<int>& v)
{
	vis[x]=1;
	for(int i=0;i<edge[x].size();i++)
	{
		int to=edge[x][i];
		if(vis[to])continue;
		for(int j=0;j<v.size();j++)
		{
			if(a[to]%v[j]==0)d[j]++;
            if(d[j]>=deep)g[to]=max(g[to],v[j]);
		}
		dfs(to,deep+1,v);
		for(int j=0;j<v.size();j++)
            if(a[to]%v[j]==0)d[j]--;
	}
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1,u,v;i<n;i++)
	{
		scanf("%d%d",&u,&v);
		edge[u].push_back(v);
		edge[v].push_back(u);
	}
	queue<int>q;
	q.push(1);g[1]=a[1];
	while(!q.empty())
	{
		int x=q.front();q.pop();
		for(int i=0;i<edge[x].size();i++)
		{
			int v=edge[x][i];
			if(fa[x]==v)continue;
			fa[v]=x;
			g[v]=f(a[v],g[x]);
			if(x==1)g[v]=a[v];
			q.push(v);
		}
	}
	vector<int> v;
    for(int i=1;i*i<=a[1];i++)
        if(a[1]%i==0)
        {
            v.push_back(i);
            if(a[1]/i!=i)v.push_back(a[1]/i);
        }
    sort(v.begin(),v.end());
    for (int i=0;i<v.size();i++)d[i]=1;
    dfs(1,1,v);
	for(int i=1;i<=n;i++)printf("%d ",g[i]);
	printf("\n");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

const int maxn=2e5+5;
struct Edge {
	int to,next;
} edge[maxn*3];
struct classcomp {
	bool operator() (const int& lhs, const int& rhs) const {
		return lhs>rhs;
	}
};
int head[maxn],tot;
int a[maxn];
int ans[maxn];
int dp[maxn];
vector<int> Set[maxn];

ll gcd(ll a,ll b) {
	return b==0?a:gcd(b,a%b);
}

void addEdge(int u,int v) {
	edge[tot].to=v;
	edge[tot].next=head[u];
	head[u]=tot++;
}

void dfs(int u,int pre) {
	for(int i=head[u]; i!=-1; i=edge[i].next) {
		int v=edge[i].to;
		if(v==pre)continue;
		dp[v]=gcd(dp[u],a[v]);
		Set[v].push_back(dp[u]);
		for(int it:Set[u]) {
			Set[v].push_back(gcd(it,a[v]));
		}
		sort(Set[v].begin(),Set[v].end());
		Set[v].erase(unique(Set[v].begin(),Set[v].end()),Set[v].end());
		dfs(v,u);
	}
}

int main() {
	int n;
	scanf("%d",&n);
	memset(head,-1,sizeof(head));
	for(int i=1; i<=n; i++) {
		scanf("%d",&a[i]);
	}
	for(int i=1; i<=n-1; i++) {
		int a,b;
		scanf("%d%d",&a,&b);
		addEdge(a,b);
		addEdge(b,a);
	}
	dp[1]=a[1];
	Set[1].push_back(0);
	dfs(1,0);
	for(int i=1; i<=n; i++) {
		ans[i]=max(dp[i],Set[i].back());
	}
	for(int i=1; i<=n; i++) {
		printf("%d ",ans[i]);
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, int> pii;
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x3f3f3f3f;
const int MAXN = 2e5 + 5;

int n;
vector<int> g[MAXN];
set<int> s[MAXN];
int a[MAXN];
typedef set<int>::iterator st;

int GCD(int a, int b) {
    return b == 0 ? a : GCD(b, a % b);
}

void dfs(int u, int fa, int cur) {
    for (st i = s[u].begin(); i != s[u].end(); i++)
        s[u].insert(GCD(*i, a[u]));
    s[u].insert(cur);
    cur = GCD(cur, a[u]);
    s[u].insert(cur);
    for (int i = 0; i < g[u].size(); i++) {
        if (g[u][i] != fa)
            dfs(g[u][i], u, cur);
    }
}

int main(int argc, char* argv[]) {
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i < n; i++) {
        int x, y; cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    dfs(1, 0, 0);

    for (int i = 1; i <= n; i++) {
        cout << *s[i].rbegin() << " ";
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;

int n,cnt;
int val[200010],head[200010],vis[200010];
int dp[200010];
vector <int> vec[200010];

int gcd(int a,int b){
	return a%b?gcd(b,a%b):b;
}

struct edge{
	int to;
	int next;
};
edge edges[500010];

void addedge(int u,int v){
	edges[cnt].to=v;
	edges[cnt].next=head[u];
	head[u]=cnt++;
}

void init(){
	for (int i=1;i<=n;i++){
		head[i]=-1;
	}
}

void dfs(int u,int fa){
	for (int i=head[u];i!=-1;i=edges[i].next){
		int v=edges[i].to;
		if (!vis[v]){
			vis[v]=1;
			dp[v]=gcd(dp[u],val[v]);
			vec[v].push_back(dp[u]);
			for (int i=0;i<vec[u].size();i++){
				vec[v].push_back(gcd(vec[u][i],val[v]));
			}
			sort(vec[v].begin(),vec[v].end());
			vec[v].erase(unique(vec[v].begin(),vec[v].end()),vec[v].end());
			dfs(v,u);
		}
	}
}

int main(){
	cin>>n;
	init();
	for (int i=1;i<=n;i++){
		scanf("%d",&val[i]);
	}
	for (int i=1;i<n;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		addedge(u,v);
		addedge(v,u);
	}
	vis[1]=1;vis[0]=1;
	dp[1]=val[1];
	vec[1].push_back(0);
	dfs(1,0);
	for (int i=1;i<=n;i++){
		printf("%d ",max(dp[i],vec[i].back()));
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include<set>
using namespace std;
typedef long long LL;
const int MAXN = 400110;

inline int readint()
{
    int ans = 0, flag = 0;
    char ch;
    while (~(ch = getchar()) && (ch < '0' || ch > '9') && ch != '-')
        ;
    ch == '-' ? flag = 1 : ans = ch - '0';
    while (~(ch = getchar()) && (ch >= '0' && ch <= '9'))
        ans = (ans << 1) + (ans << 3) + ch - '0';
    (flag) && (ans = -ans);
    return ans;
}

inline void print(int x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        print(x / 10);
    putchar(x % 10 + '0');
    /* printf("%d",x);*/
}

int gcd(int a, int b)
{
    int t = a % b;
    while (t)
    {
        a = b, b = t, t = a % b;
    }
    return b;
}
/*建无向图*/
int n, m;
int g[MAXN], to[MAXN], v[MAXN], nextn[MAXN], eid = 0;
void addedge(int x, int y)
{
    to[++eid] = y, nextn[eid] = g[x], g[x] = eid;
    to[++eid] = x, nextn[eid] = g[y], g[y] = eid;
}

/*建树*/
int depth[MAXN], fa[MAXN]; //fa[x]表示第x个节点祖先，不存在用-1表示
int dp1[MAXN];           //dp1[i]还未删点的最大gcd
int ans[MAXN];

set<int> dp2[MAXN];//dp2[i]已删点的最大gcd
set<int>::iterator it;
void dfs(int nfa, int nu) //以nu为根的子树，其父节点为nfa
{
    //printf("nfa=%d nu=%d\n",nfa,nu);
    if (nfa != 0)
        depth[nu] = depth[nfa] + 1, dp1[nu] = gcd(dp1[nfa], v[nu]); //树的逻辑深度
    else
        depth[nu] = 1, dp1[nu] = v[nu],ans[nu]=v[nu];
    fa[nu] = nfa;
    ans[nu]=dp1[nu];
    if (nu != 1)
    {
        if (dp2[nfa].size() == 0)
        {
            dp2[nu].insert(dp1[nfa]);
            dp2[nu].insert(v[nu]);
            ans[nu]=max(ans[nu],dp1[nfa]);
            ans[nu]=max(ans[nu],v[nu]);
        }
        else
        {
            dp2[nu].insert(dp1[nfa]);
            ans[nu]=max(ans[nu],dp1[nfa]);
            int maxt=0,t;
            for(it=dp2[nfa].begin();it!=dp2[nfa].end();it++)
            {
                t=gcd(*it, v[nu]);
                dp2[nu].insert(t);
                maxt=max(maxt,t);
            }
            ans[nu]=max(ans[nu],maxt);
        }
    }
    for (int i = g[nu]; i != -1; i = nextn[i])
    {
        if (to[i] != nfa)
        {
            //printf("i=%d nfa=%d nu=%d to=%d \n",i,nfa,nu,to[i]);
            dfs(nu, to[i]);
        }
    }
}
void buildtree(int root)
{
    memset(fa, 0, sizeof(fa));
    memset(depth, 0, sizeof(depth));
    dfs(0, root);
}

int main()
{
    n = readint();
    int x, y;
    for (int i = 1; i <= n; i++)
        v[i] = readint();
    memset(nextn, -1, sizeof(nextn));
    memset(g, -1, sizeof(g));

    for (int i = 1; i < n; i++)
    {
        x = readint();
        y = readint();
        addedge(x, y);
    }
    int root = 1;
    buildtree(root);
    for (int i = 1; i <= n; i++)
    {
        printf("%d ", ans[i]);
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define x first
#define y second
#define debug(...) cout << "[" << #__VA_ARGS__ << ": " << __VA_ARGS__ << "]\n"
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, ll>pii;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, val[maxn], ans[maxn], cnt[maxn], g[maxn];
vector<int>adj[maxn];
vector<int>divs;
void dfs(int v, int p, int dep)
{
    for(int d : divs)
        if(val[v] % d == 0)
            cnt[d]++;
    for(int d : divs)
        if(cnt[d] >= dep)
            ans[v] = max(ans[v], d);
    for(int to : adj[v]) if(to != p)
        {
            g[to] = __gcd(g[v], val[to]);
            dfs(to, v, dep + 1);
        }
    if(v != 1)
    {
        for(int d : divs)
            if(val[v] % d == 0)
                cnt[d]--;
    }
}
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> val[i];
    for(int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    for(int i = 1; i * i <= val[1]; i++)
    {
        if(val[1] % i == 0) divs.pb(i);
        if(i * i != val[1]) divs.pb(val[1] / i);
    }
    dfs(1, -1, 0);
    for(int i = 1; i <= n; i++)
        cout << max(g[i], ans[i]) << " ";

    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define x first
#define y second
#define debug(...) cout << "[" << #__VA_ARGS__ << ": " << __VA_ARGS__ << "]\n"
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, ll>pii;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, val[maxn], ans[maxn], cnt[maxn];
vector<int>adj[maxn];
vector<int>divs;
void dfs1(int v, int p, int gcd)
{
    if(v != 1) gcd = __gcd(gcd, val[v]);
    ans[v] = max(ans[v], gcd);
    for(int to : adj[v]) if(to != p)
        dfs1(to, v, gcd);
}
void dfs(int v, int p, int dep)
{
    if(v != 1)
    {
        for(int d : divs)
            if(val[v] % d == 0 || d % val[v] == 0)
                cnt[d]++;
        for(int d : divs)
            if(cnt[d] >= dep - 1)
                ans[v] = max(ans[v], d);
    }
    for(int to : adj[v]) if(to != p)
        dfs(to, v, dep + 1);
    if(v != 1)
    {
        for(int d : divs)
            if(val[v] % d == 0 || d % val[v] == 0)
                cnt[d]--;
    }
}
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> val[i];
    for(int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    ans[1] = val[1];
    dfs1(1, -1, 0);
    for(int i = 1; i * i <= val[1]; i++)
    {
        if(val[1] % i == 0) divs.pb(i);
        if(i * i != val[1]) divs.pb(val[1] / i);
    }
    dfs(1, -1, 0);
    for(int i = 1; i <= n; i++)
        cout << ans[i] << " ";
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define x first
#define y second
#define debug(...) cout << "[" << #__VA_ARGS__ << ": " << __VA_ARGS__ << "]\n"
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, ll>pii;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, val[maxn], ans[maxn], cnt[maxn], g[maxn];
vector<int>adj[maxn];
vector<int>divs;
void dfs(int v, int p, int dep)
{
    if(v != 1)
    {
        for(int d : divs)
            if(val[v] % d == 0)
                cnt[d]++;
        for(int d : divs)
            if(cnt[d] >= dep - 1)
                ans[v] = max(ans[v], d);
    }
    for(int to : adj[v]) if(to != p)
    {
        g[to] = __gcd(g[v], val[to]);
        dfs(to, v, dep + 1);
    }
    if(v != 1)
    {
        for(int d : divs)
            if(val[v] % d == 0)
                cnt[d]--;
    }
}
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> val[i];
    for(int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    ans[1] = val[1];
    for(int i = 1; i * i <= val[1]; i++)
    {
        if(val[1] % i == 0) divs.pb(i);
        if(i * i != val[1]) divs.pb(val[1] / i);
    }
    dfs(1, -1, 0);
    for(int i = 1; i <= n; i++)
        cout << max(g[i], ans[i]) << " ";

    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define x first
#define y second
#define debug(...) cout << "[" << #__VA_ARGS__ << ": " << __VA_ARGS__ << "]\n"
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, ll>pii;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, val[maxn], ans[maxn];
vector<int>adj[maxn];
map<int, int>vals;
void dfs(int v, int p, int gcd, bool ignored)
{
    vals[val[v]]++;
    int gcd1 = -1, gcd2 = -1;
    bool ign = ignored;
    if(__gcd(gcd, val[v]) == 1 && !ignored)
    {
        if(p == 1)
        {
            gcd1 = val[v];
            gcd2 = val[1];
            ign = true;
        }
        else gcd1 = gcd;
    }
    gcd = __gcd(gcd, val[v]);
    if(ign)
    {
        if(gcd1 != -1) ans[v] = max(ans[v], gcd1);
        if(gcd2 != -1) ans[v] = max(ans[v], gcd2);
        if(gcd1 == -1) ans[v] = max(ans[v], gcd);
    }
    else
    {
        if(v != 1 && vals.begin()->y == 1) ans[v] = max(ans[v], next(vals.begin())->x);
        else ans[v] = max(ans[v], gcd);
    }
    for(int to : adj[v])
    {
        if(to != p)
        {
            if(gcd1 != -1) dfs(to, v, gcd1, ign);
            if(gcd2 != -1) dfs(to, v, gcd2, ign);
            dfs(to, v, gcd, ign);
        }
    }
    if(--vals[val[v]] == 0) vals.erase(vals.find(val[v]));
}
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> val[i];
    for(int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    dfs(1, -1, 0, false);
    for(int i = 1; i <= n; i++)
        cout << ans[i] << " ";
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define x first
#define y second
#define debug(...) cout << "[" << #__VA_ARGS__ << ": " << __VA_ARGS__ << "]\n"
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, ll>pii;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, val[maxn], ans[maxn], g[maxn];
vector<int>adj[maxn];
void dfs(int v, int p, set<int>s)
{
    set<int>ss;
    for(int aa : s)
    {
        ans[v] = max(ans[v], __gcd(aa, val[v]));
        ss.insert(__gcd(aa, val[v]));
    }
    ss.insert(g[p]);
    ans[v] = max(ans[v], g[p]);
    g[v] = __gcd(g[p], val[v]);
    for(int to : adj[v]) if(to != p)
            dfs(to, v, ss);
}
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> val[i];
    for(int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    ans[1] = val[1];
    g[1] = val[1];
    dfs(1, 0, {});
    for(int i = 1; i <= n; i++)
        cout << ans[i] << " ";
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define x first
#define y second
#define debug(...) cout << "[" << #__VA_ARGS__ << ": " << __VA_ARGS__ << "]\n"
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, ll>pii;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, val[maxn], ans[maxn], cnt[maxn];
vector<int>adj[maxn];
vector<int>divs;
void dfs1(int v, int p, int gcd)
{
    if(v != 1) gcd = __gcd(gcd, val[v]);
    ans[v] = max(ans[v], gcd);
    for(int to : adj[v]) if(to != p)
        dfs1(to, v, gcd);
}
void dfs(int v, int p, int dep)
{
    if(v != 1)
    {
        for(int d : divs)
            if(d % val[v] == 0)
                cnt[d]++;
        for(int d : divs)
            if(cnt[d] >= dep - 1)
                ans[v] = max(ans[v], d);
    }
    for(int to : adj[v]) if(to != p)
        dfs(to, v, dep + 1);
    if(v != 1)
    {
        for(int d : divs)
            if(d % val[v] == 0)
                cnt[d]--;
    }
}
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> val[i];
    for(int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    ans[1] = val[1];
    dfs1(1, -1, 0);
    for(int i = 1; i * i <= val[1]; i++)
    {
        if(val[1] % i == 0) divs.pb(i);
        if(i * i != val[1]) divs.pb(val[1] / i);
    }
    dfs(1, -1, 0);
    for(int i = 1; i <= n; i++)
        cout << ans[i] << " ";
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define x first
#define y second
#define debug(...) cout << "[" << #__VA_ARGS__ << ": " << __VA_ARGS__ << "]\n"
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, ll>pii;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, val[maxn], ans[maxn];
vector<int>adj[maxn];
map<int, int>vals;
void dfs(int v, int p, int gcd, bool ignored)
{
    vals[val[v]]++;
    int gcd1 = -1, gcd2 = -1;
    bool ign = ignored;
    if(__gcd(gcd, val[v]) == 1 && !ign)
    {
        ign = true;
        if(p == 1)
        {
            gcd1 = val[v];
            gcd2 = val[1];
        }
        else  gcd1 = gcd;
    }
    gcd = __gcd(gcd, val[v]);
    if(ign)
    {
        if(gcd1 != -1) ans[v] = max(ans[v], gcd1);
        if(gcd2 != -1) ans[v] = max(ans[v], gcd2);
        if(gcd1 == -1) ans[v] = max(ans[v], gcd);
    }
    else
    {
        if(v != 1 && vals.begin()->y == 1) ans[v] = max(ans[v], next(vals.begin())->x);
        else ans[v] = max(ans[v], gcd);
    }
    for(int to : adj[v])
    {
        if(to != p)
        {
            if(gcd1 != -1) dfs(to, v, gcd1, ign);
            if(gcd2 != -1) dfs(to, v, gcd2, ign);
            dfs(to, v, gcd, ign);
        }
    }
    if(--vals[val[v]] == 0) vals.erase(vals.find(val[v]));
}
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> val[i];
    for(int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    dfs(1, -1, 0, false);
    for(int i = 1; i <= n; i++)
        cout << ans[i] << " ";
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define x first
#define y second
#define debug(...) cout << "[" << #__VA_ARGS__ << ": " << __VA_ARGS__ << "]\n"
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, ll>pii;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, val[maxn], ans[maxn], cnt[maxn];
vector<int>adj[maxn];
vector<int>divs;
void dfs1(int v, int p, int gcd)
{
    if(v != 1) gcd = __gcd(gcd, val[v]);
    ans[v] = max(ans[v], gcd);
    for(int to : adj[v]) if(to != p)
        dfs1(to, v, gcd);
}
void dfs(int v, int p, int dep)
{
    if(v != 1)
    {
        for(int d : divs)
            if(__gcd(d, val[v]) == d)
                cnt[d]++;
        for(int d : divs)
            if(cnt[d] >= dep - 1)
                ans[v] = max(ans[v], d);
    }
    for(int to : adj[v]) if(to != p)
        dfs(to, v, dep + 1);
    if(v != 1)
    {
        for(int d : divs)
            if(__gcd(d, val[v]) == d)
                cnt[d]--;
    }
}
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> val[i];
    for(int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    ans[1] = val[1];
    dfs1(1, -1, 0);
    for(int i = 1; i * i <= val[1]; i++)
    {
        if(val[1] % i == 0) divs.pb(i);
        if(i * i != val[1]) divs.pb(val[1] / i);
    }
    dfs(1, -1, 0);
    for(int i = 1; i <= n; i++)
        cout << ans[i] << " ";
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define x first
#define y second
#define debug(...) cout << "[" << #__VA_ARGS__ << ": " << __VA_ARGS__ << "]\n"
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, ll>pii;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, val[maxn], ans[maxn], cnt[maxn], g[maxn];
vector<int>adj[maxn];
vector<int>divs;
void dfs(int v, int p, int dep)
{
    if(v != 1)
    {
        for(int d : divs)
            if(val[v] % d == 0)
                cnt[d]++;
        for(int d : divs)
            if(cnt[d] >= dep - 1)
                ans[v] = max(ans[v], d);
    }
    for(int to : adj[v]) if(to != p)
    {
        dfs(to, v, dep + 1);
        g[to] = __gcd(g[v], val[to]);
    }
    if(v != 1)
    {
        for(int d : divs)
            if(val[v] % d == 0)
                cnt[d]--;
    }
}
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> val[i];
    for(int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    ans[1] = val[1];
    for(int i = 1; i * i <= val[1]; i++)
    {
        if(val[1] % i == 0) divs.pb(i);
        if(i * i != val[1]) divs.pb(val[1] / i);
    }
    dfs(1, -1, 0);
    for(int i = 1; i <= n; i++)
        cout << max(g[i], ans[i]) << " ";
    cout << "\n";
    return 0;
}


/*Let's get high :D*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define MOD                 1000000007LL
#define EPS                 1e-9
#define io                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI                3.14159265358979323846

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

const int MAXN = 2e5+5;
int arr[MAXN];
int n;
vector<int> adj[MAXN];
int beauty[MAXN];
int vis[MAXN];

void bfs(){
    queue<pair<int, pair<int, int > > > q;
    q.push({1, {arr[1], 0}});
    q.push({1, {0, 1}});
    beauty[1] = arr[1];
    while(!q.empty()){
        auto p = q.front();q.pop();
        int u = p.first;
        int gc = p.second.first;
        int modified = p.second.second;
        for(auto v : adj[u]){
            if(vis[v] < 2){
                if(modified == 1){
                    beauty[v] = max(beauty[v], gcd(gc, arr[v]));
                    q.push({v, {beauty[v], 1}});
                    vis[v]++;
                }else{
                    // can change
                    int gcd1 = gcd(gc, arr[v]);
                    int gcd2 = gc;
                    if(gcd1 < gcd2){
                        // change
                        beauty[v] = max(beauty[v], gcd2);
                        q.push({v, {beauty[v], 1}});
                        q.push({v, {gcd1, 0}});
                        vis[v]++;
                    }else{
                        // lite
                        beauty[v] = max(beauty[v], gcd1);
                        q.push({v, {beauty[v], 0}});
                        vis[v]++;
                    }
                }
            }
        }
    }
    for(int i = 1;i <= n; i++)
        cout << beauty[i] << " ";
}

int main(){
    io;
    // int n;
    cin >> n;
    for(int i = 1;i <= n; i++)
        cin >> arr[i];
    for(int i = 0;i < n-1; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    bfs();
    return 0;
}


/*Let's get high :D*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define MOD                 1000000007LL
#define EPS                 1e-9
#define io                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI                3.14159265358979323846

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

const int MAXN = 2e5+5;
int arr[MAXN];
int n;
vector<int> adj[MAXN];
int beauty[MAXN];
int parent[MAXN];
int vis[MAXN];

void bfs(){
    queue<pair<int, pair<int, int > > > q;
    q.push({1, {arr[1], 0}});
    q.push({1, {0, 1}});
    beauty[1] = arr[1];
    while(!q.empty()){
        auto p = q.front();q.pop();
        int u = p.first;
        int gc = p.second.first;
        int modified = p.second.second;
        // cout << u << " " << gc << " " << modified << endl;
        for(auto v : adj[u]){
            if(v == parent[u])  continue;
            if(vis[v] < n){
                if(modified == 1){
                    beauty[v] = max(beauty[v], gcd(gc, arr[v]));
                    q.push({v, {beauty[v], 1}});
                    vis[v]++;
                }else{
                    // can change
                    int gcd1 = gcd(gc, arr[v]);
                    int gcd2 = gc;
                    if(gcd1 < gcd2){
                        // change
                        beauty[v] = max(beauty[v], gcd2);
                        q.push({v, {beauty[v], 1}});
                        q.push({v, {gcd1, 0}});
                        vis[v]++;
                    }else{
                        // lite
                        beauty[v] = max(beauty[v], gcd1);
                        q.push({v, {beauty[v], 0}});
                        vis[v]++;
                    }
                }
            }
            parent[v] = u;
        }
    }
    for(int i = 1;i <= n; i++)
        cout << beauty[i] << " ";
}

int main(){
    io;
    cin >> n;
    for(int i = 1;i <= n; i++)
        cin >> arr[i];
    for(int i = 0;i < n-1; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    bfs();
    return 0;
}


/*Let's get high :D*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define MOD                 1000000007LL
#define EPS                 1e-9
#define io                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI                3.14159265358979323846

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

const int MAXN = 2e5+5;
int arr[MAXN];
int n;
vector<int> adj[MAXN];
int beauty[MAXN];
int parent[MAXN];
int vis[MAXN];

void bfs(){
    queue<pair<int, pair<int, int > > > q;
    q.push({1, {arr[1], 0}});
    q.push({1, {0, 1}});
    beauty[1] = arr[1];
    while(!q.empty()){
        auto p = q.front();q.pop();
        int u = p.first;
        int gc = p.second.first;
        int modified = p.second.second;
        // cout << u << " " << gc << " " << modified << endl;
        for(auto v : adj[u]){
            if(v == parent[u])  continue;
            if(vis[v] <= n){
                if(modified == 1){
                    beauty[v] = max(beauty[v], gcd(gc, arr[v]));
                    // cout << "insert " << v << " " << beauty[v] << " 1" << endl;
                    q.push({v, {gcd(gc, arr[v]), 1}});
                    vis[v]++;
                }else{
                    // can change
                    int gcd1 = gcd(gc, arr[v]);
                    int gcd2 = gc;
                    if(gcd1 < gcd2){
                        // change
                        beauty[v] = max(beauty[v], gcd2);
                        // cout << "insert " << v << " " << beauty[v] << " 1" << endl;
                        q.push({v, {beauty[v], 1}});
                        // cout << "insert " << v << " " << gcd1 << " 0" << endl;
                        q.push({v, {gcd1, 0}});
                        vis[v]++;
                    }else{
                        // lite
                        beauty[v] = max(beauty[v], gcd1);
                        // cout << "insert " << v << " " << beauty[v] << " 0" << endl;
                        q.push({v, {gcd1, 0}});
                        vis[v]++;
                    }
                }
            }
            parent[v] = u;
        }
    }
    for(int i = 1;i <= n; i++)
        cout << beauty[i] << " ";
}

int main(){
    io;
    cin >> n;
    for(int i = 1;i <= n; i++)
        cin >> arr[i];
    for(int i = 0;i < n-1; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    bfs();
    return 0;
}


/*Let's get high :D*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define MOD                 1000000007LL
#define EPS                 1e-9
#define io                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI                3.14159265358979323846

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

const int MAXN = 2e5+5;
int arr[MAXN];
int n;
vector<int> adj[MAXN];
int beauty[MAXN];
int parent[MAXN];
int vis[MAXN];

void bfs(){
    queue<pair<int, pair<int, int > > > q;
    q.push({1, {arr[1], 0}});
    q.push({1, {0, 1}});
    beauty[1] = arr[1];
    while(!q.empty()){
        auto p = q.front();q.pop();
        int u = p.first;
        int gc = p.second.first;
        int modified = p.second.second;
        // cout << u << " " << gc << " " << modified << endl;
        for(auto v : adj[u]){
            if(v == parent[u])  continue;
            if(vis[v] <= n){
                if(modified == 1){
                    beauty[v] = max(beauty[v], gcd(gc, arr[v]));
                    // cout << "insert " << v << " " << beauty[v] << " 1" << endl;
                    q.push({v, {beauty[v], 1}});
                    vis[v]++;
                }else{
                    // can change
                    int gcd1 = gcd(gc, arr[v]);
                    int gcd2 = gc;
                    if(gcd1 < gcd2){
                        // change
                        beauty[v] = max(beauty[v], gcd2);
                        // cout << "insert " << v << " " << beauty[v] << " 1" << endl;
                        q.push({v, {beauty[v], 1}});
                        // cout << "insert " << v << " " << gcd1 << " 0" << endl;
                        q.push({v, {gcd1, 0}});
                        vis[v]++;
                    }else{
                        // lite
                        beauty[v] = max(beauty[v], gcd1);
                        // cout << "insert " << v << " " << beauty[v] << " 0" << endl;
                        q.push({v, {gcd1, 0}});
                        vis[v]++;
                    }
                }
            }
            parent[v] = u;
        }
    }
    for(int i = 1;i <= n; i++)
        cout << beauty[i] << " ";
}

int main(){
    io;
    cin >> n;
    for(int i = 1;i <= n; i++)
        cin >> arr[i];
    for(int i = 0;i < n-1; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    bfs();
    return 0;
}


/*Let's get high :D*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define MOD                 1000000007LL
#define EPS                 1e-9
#define io                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI                3.14159265358979323846

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

const int MAXN = 2e5+5;
int arr[MAXN];
int n;
vector<int> adj[MAXN];
int beauty[MAXN];
int vis[MAXN];

void bfs(){
    queue<pair<int, pair<int, int > > > q;
    q.push({1, {arr[1], 0}});
    beauty[1] = arr[1];
    while(!q.empty()){
        auto p = q.front();q.pop();
        int u = p.first;
        // cout << u << endl;
        int gc = p.second.first;
        int modified = p.second.second;
        for(auto v : adj[u]){
            if(!vis[v]){
                if(modified == 1){
                    // already done
                    beauty[v] = gcd(gc, arr[v]);
                    q.push({v, {beauty[v], 1}});
                    vis[v] = 1;
                }else{
                    // can change
                    int gcd1 = gcd(gc, arr[v]);
                    int gcd2 = gc;
                    if(gcd1 < gcd2){
                        // change
                        beauty[v] = gcd2;
                        q.push({v, {beauty[v], 1}});
                        q.push({v, {gcd1, 0}});
                        vis[v] = 1;
                    }else{
                        // lite
                        beauty[v] = gcd1;
                        q.push({v, {beauty[v], 0}});
                        vis[v] = 1;
                    }
                }
            }
        }
    }
    for(int i = 1;i <= n; i++)
        cout << beauty[i] << " ";
}

int main(){
    io;
    // int n;
    cin >> n;
    for(int i = 1;i <= n; i++)
        cin >> arr[i];
    for(int i = 0;i < n-1; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    bfs();
    return 0;
}


/*Let's get high :D*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define MOD                 1000000007LL
#define EPS                 1e-9
#define io                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI                3.14159265358979323846

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

const int MAXN = 2e5+5;
int arr[MAXN];
int n;
vector<int> adj[MAXN];
int beauty[MAXN];
int parent[MAXN];
int vis[MAXN];

void bfs(){
    queue<pair<int, pair<int, int > > > q;
    q.push({1, {arr[1], 0}});
    q.push({1, {0, 1}});
    beauty[1] = arr[1];
    while(!q.empty()){
        auto p = q.front();q.pop();
        int u = p.first;
        int gc = p.second.first;
        int modified = p.second.second;
        // cout << u << " " << gc << " " << modified << endl;
        for(auto v : adj[u]){
            if(v == parent[u])  continue;
            if(vis[v] <= n){
                if(modified == 1){
                    beauty[v] = max(beauty[v], gcd(gc, arr[v]));
                    // cout << "insert " << v << " " << beauty[v] << " 1" << endl;
                    q.push({v, {gcd(gc, arr[v]), 1}});
                    vis[v]++;
                }else{
                    // can change
                    int gcd1 = gcd(gc, arr[v]);
                    int gcd2 = gc;
                    if(gcd1 < gcd2){
                        // change
                        beauty[v] = max(beauty[v], gcd2);
                        // cout << "insert " << v << " " << beauty[v] << " 1" << endl;
                        q.push({v, {gcd2, 1}});
                        // cout << "insert " << v << " " << gcd1 << " 0" << endl;
                        q.push({v, {gcd1, 0}});
                        vis[v]++;
                    }else{
                        // lite
                        beauty[v] = max(beauty[v], gcd1);
                        // cout << "insert " << v << " " << beauty[v] << " 0" << endl;
                        q.push({v, {gcd1, 0}});
                        vis[v]++;
                    }
                }
            }
            parent[v] = u;
        }
    }
    for(int i = 1;i <= n; i++)
        cout << beauty[i] << " ";
}

int main(){
    io;
    cin >> n;
    for(int i = 1;i <= n; i++)
        cin >> arr[i];
    for(int i = 0;i < n-1; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    bfs();
    return 0;
}


/*Let's get high :D*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define MOD                 1000000007LL
#define EPS                 1e-9
#define io                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI                3.14159265358979323846

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

const int MAXN = 2e5+5;
int arr[MAXN];
int n;
vector<int> adj[MAXN];
int beauty[MAXN];
int parent[MAXN];
int vis[MAXN];

void bfs(){
    queue<pair<int, pair<int, int > > > q;
    q.push({1, {arr[1], 0}});
    q.push({1, {0, 1}});
    beauty[1] = arr[1];
    while(!q.empty()){
        auto p = q.front();q.pop();
        int u = p.first;
        int gc = p.second.first;
        int modified = p.second.second;
        for(auto v : adj[u]){
            // if(v == parent[u])  continue;
            if(vis[v] <= n){
                if(modified == 1){
                    beauty[v] = max(beauty[v], gcd(gc, arr[v]));
                    q.push({v, {beauty[v], 1}});
                    vis[v]++;
                }else{
                    // can change
                    int gcd1 = gcd(gc, arr[v]);
                    int gcd2 = gc;
                    if(gcd1 < gcd2){
                        // change
                        beauty[v] = max(beauty[v], gcd2);
                        q.push({v, {beauty[v], 1}});
                        q.push({v, {gcd1, 0}});
                        vis[v]++;
                    }else{
                        // lite
                        beauty[v] = max(beauty[v], gcd1);
                        q.push({v, {beauty[v], 0}});
                        vis[v]++;
                    }
                }
            }
            // parent[v] = u;
        }
    }
    for(int i = 1;i <= n; i++)
        cout << beauty[i] << " ";
}

int main(){
    io;
    cin >> n;
    for(int i = 1;i <= n; i++)
        cin >> arr[i];
    for(int i = 0;i < n-1; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    bfs();
    return 0;
}


/*Let's get high :D*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define MOD                 1000000007LL
#define EPS                 1e-9
#define io                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI                3.14159265358979323846

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

const int MAXN = 2e5+5;
int arr[MAXN];
int n;
vector<int> adj[MAXN];
int beauty[MAXN];
int parent[MAXN];
int vis[MAXN];

void bfs(){
    queue<pair<int, pair<int, int > > > q;
    q.push({1, {arr[1], 0}});
    q.push({1, {0, 1}});
    beauty[1] = arr[1];
    while(!q.empty()){
        auto p = q.front();q.pop();
        int u = p.first;
        int gc = p.second.first;
        int modified = p.second.second;
        for(auto v : adj[u]){
            if(v == parent[u])  continue;
            if(vis[v] < 2){
                if(modified == 1){
                    beauty[v] = max(beauty[v], gcd(gc, arr[v]));
                    q.push({v, {beauty[v], 1}});
                    vis[v]++;
                }else{
                    // can change
                    int gcd1 = gcd(gc, arr[v]);
                    int gcd2 = gc;
                    if(gcd1 < gcd2){
                        // change
                        beauty[v] = max(beauty[v], gcd2);
                        q.push({v, {beauty[v], 1}});
                        q.push({v, {gcd1, 0}});
                        vis[v]++;
                    }else{
                        // lite
                        beauty[v] = max(beauty[v], gcd1);
                        q.push({v, {beauty[v], 0}});
                        vis[v]++;
                    }
                }
            }
            parent[v] = u;
        }
    }
    for(int i = 1;i <= n; i++)
        cout << beauty[i] << " ";
}

int main(){
    io;
    // int n;
    cin >> n;
    for(int i = 1;i <= n; i++)
        cin >> arr[i];
    for(int i = 0;i < n-1; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    bfs();
    return 0;
}


/*Let's get high :D*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define MOD                 1000000007LL
#define EPS                 1e-9
#define io                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI                3.14159265358979323846

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

const int MAXN = 2e5+5;
int arr[MAXN];
int n;
vector<int> adj[MAXN];
int beauty[MAXN];
int parent[MAXN];
int vis[MAXN];

void bfs(){
    queue<pair<int, pair<int, int > > > q;
    q.push({1, {arr[1], 0}});
    q.push({1, {0, 1}});
    beauty[1] = arr[1];
    while(!q.empty()){
        auto p = q.front();q.pop();
        int u = p.first;
        int gc = p.second.first;
        int modified = p.second.second;
        // cout << u << " " << gc << " " << modified << endl;
        for(auto v : adj[u]){
            if(v == parent[u])  continue;
            if(vis[v] <= n){
                if(modified == 1){
                    beauty[v] = max(beauty[v], gcd(gc, arr[v]));
                    q.push({v, {beauty[v], 1}});
                    vis[v]++;
                }else{
                    // can change
                    int gcd1 = gcd(gc, arr[v]);
                    int gcd2 = gc;
                    if(gcd1 < gcd2){
                        // change
                        beauty[v] = max(beauty[v], gcd2);
                        q.push({v, {beauty[v], 1}});
                        q.push({v, {gcd1, 0}});
                        vis[v]++;
                    }else{
                        // lite
                        beauty[v] = max(beauty[v], gcd1);
                        q.push({v, {beauty[v], 0}});
                        vis[v]++;
                    }
                }
            }
            parent[v] = u;
        }
    }
    for(int i = 1;i <= n; i++)
        cout << beauty[i] << " ";
}

int main(){
    io;
    cin >> n;
    for(int i = 1;i <= n; i++)
        cin >> arr[i];
    for(int i = 0;i < n-1; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    bfs();
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
#define int long long int
void fun(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
   #endif
}
int md=1e9+7;
int __gcd(int a, int b) {if(b==0) return a; return __gcd(b, a%b);}
int poww(int a, int b) {int res=1;while(b) {if(b&1){res*=a;res%=md;}a*=a;a%=md;b>>=1;}return res;}
void ainp(int arr[], int n) {for(int i=1;i<=n;i++) cin>>arr[i];}
vector<int> v[200001];
int beauty[200001];
int arr[200001];
void dfs1(int par, int gpar, int gd)
{
	int gc=__gcd(arr[par], gd);
	beauty[par]=gc;
	if(par==1) gc=0;
	for(auto itr:v[par])
	{
		if(itr==gpar) continue;
		dfs1(itr, par, gc);
	}
}
void dfs2(int par, int gpar, map<int, int> mapp, int depth)
{
	int ans=1;
	for(auto itr:mapp)
	{
		if(arr[par]%itr.first==0)
			itr.second++;
		if(itr.second>=depth) ans=max(ans, itr.first);
	}
	beauty[par]=max(beauty[par], ans);
	for(auto itr:v[par])
	{
		if(itr==gpar) continue;
		dfs2(itr, par, mapp, depth+1);
	}
}
signed main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);fun();
	int n; cin>>n; ainp(arr, n);
	int a, b;
	for(int i=0;i<n-1;i++)
	{
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	dfs1(1, 0, 0);
	map<int, int> mapp;
	for(int i=1;i*i<=arr[1];i++)
	{
		if(arr[1]%i==0)
			mapp[i]++;
		if(arr[1]/i!=i)
			mapp[arr[1]/i]++;
	}
	for(auto itr:v[1])
	{
		dfs2(itr, 1, mapp, 1);
	}
	for(int i=1;i<=n;i++)
	{
		cout<<beauty[i]<<" ";
	}
	return 0;
}