#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
int n;
vector<int>ans;
vector<bool>vis;
vector<int>mas;
vector<int>del;
vector<int>koll;
vector<vector<int>>edges;
int nod(int a, int b)
{
if (b == 0)
return a;
return nod(b, a % b);
}
void dfs1(int v)
{
vis[v] = 1;
for (auto u : edges[v])
if (! vis[u])
{
ans[u] = nod(ans[v], mas[u]);
dfs1(u);
}
}
void dfs2(int v, int dist)
{
vis[v] = 1;
for (int i = 0; i < del.size(); i++)
{
koll[i] += (mas[v] % del[i] == 0);
if (koll[i] >= dist)
ans[v] = max(ans[v], del[i]);
}
for (auto u : edges[v])
if (! vis[u])
dfs2(u, dist + 1);
for (int i = 0; i < del.size(); i++)
koll[i] -= (mas[v] % del[i] == 0);
}
signed main()
{
ios_base::sync_with_stdio(0);
int n;
cin>>n;
ans.resize(n);
vis.resize(n);
mas.resize(n);
edges.resize(n);
for (int i = 0; i < n; i++)
cin>>mas[i];
for (int i = 0; i < n - 1; i++)
{
int a, b;
cin>>a>>b;
a--; b--;
edges[a].push_back(b);
edges[b].push_back(a);
}
int p = mas[0];
mas[0] = 0;
ans[0] = 0;
dfs1(0);
mas[0] = p;
for (int i = 0; i < n; i++)
vis[i] = 0;
for (int i = 1; i*i <= mas[0]; i++)
{
if (mas[0] % i == 0)
{
del.push_back(i);
del.push_back(mas[0] / i);
if (i*i == mas[0])
del.pop_back();
}
}
sort(del.begin(), del.end());
koll.resize(del.size());
dfs2(0, 0);
for (int i = 0; i < n; i++)
cout<<ans[i]<<' ';
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBmIGZpcnN0CiNkZWZpbmUgcyBzZWNvbmQKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBuOwp2ZWN0b3I8aW50PmFuczsKdmVjdG9yPGJvb2w+dmlzOwp2ZWN0b3I8aW50Pm1hczsKdmVjdG9yPGludD5kZWw7CnZlY3RvcjxpbnQ+a29sbDsKdmVjdG9yPHZlY3RvcjxpbnQ+PmVkZ2VzOwoKaW50IG5vZChpbnQgYSwgaW50IGIpCnsKICAgIGlmIChiID09IDApCiAgICAgICAgcmV0dXJuIGE7CiAgICByZXR1cm4gbm9kKGIsIGEgJSBiKTsKfQoKdm9pZCBkZnMxKGludCB2KQp7CiAgICB2aXNbdl0gPSAxOwogICAgZm9yIChhdXRvIHUgOiBlZGdlc1t2XSkKICAgICAgICBpZiAoISB2aXNbdV0pCiAgICAgICAgewogICAgICAgICAgICBhbnNbdV0gPSBub2QoYW5zW3ZdLCBtYXNbdV0pOwogICAgICAgICAgICBkZnMxKHUpOwogICAgICAgIH0KfQoKdm9pZCBkZnMyKGludCB2LCBpbnQgZGlzdCkKewogICAgdmlzW3ZdID0gMTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgZGVsLnNpemUoKTsgaSsrKQogICAgewogICAgICAgIGtvbGxbaV0gKz0gKG1hc1t2XSAlIGRlbFtpXSA9PSAwKTsKICAgICAgICBpZiAoa29sbFtpXSA+PSBkaXN0KQogICAgICAgICAgICBhbnNbdl0gPSBtYXgoYW5zW3ZdLCBkZWxbaV0pOwogICAgfQogICAgZm9yIChhdXRvIHUgOiBlZGdlc1t2XSkKICAgICAgICBpZiAoISB2aXNbdV0pCiAgICAgICAgICAgIGRmczIodSwgZGlzdCArIDEpOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBkZWwuc2l6ZSgpOyBpKyspCiAgICAgICAga29sbFtpXSAtPSAobWFzW3ZdICUgZGVsW2ldID09IDApOwp9CgpzaWduZWQgbWFpbigpCnsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgICBpbnQgbjsKICAgIGNpbj4+bjsKICAgIGFucy5yZXNpemUobik7CiAgICB2aXMucmVzaXplKG4pOwogICAgbWFzLnJlc2l6ZShuKTsKICAgIGVkZ2VzLnJlc2l6ZShuKTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQogICAgICAgIGNpbj4+bWFzW2ldOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuIC0gMTsgaSsrKQogICAgewogICAgICAgIGludCBhLCBiOwogICAgICAgIGNpbj4+YT4+YjsKICAgICAgICBhLS07IGItLTsKICAgICAgICBlZGdlc1thXS5wdXNoX2JhY2soYik7CiAgICAgICAgZWRnZXNbYl0ucHVzaF9iYWNrKGEpOwogICAgfQogICAgaW50IHAgPSBtYXNbMF07CiAgICBtYXNbMF0gPSAwOwogICAgYW5zWzBdID0gMDsKICAgIGRmczEoMCk7CiAgICBtYXNbMF0gPSBwOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgdmlzW2ldID0gMDsKICAgIGZvciAoaW50IGkgPSAxOyBpKmkgPD0gbWFzWzBdOyBpKyspCiAgICB7CiAgICAgICAgaWYgKG1hc1swXSAlIGkgPT0gMCkKICAgICAgICB7CiAgICAgICAgICAgIGRlbC5wdXNoX2JhY2soaSk7CiAgICAgICAgICAgIGRlbC5wdXNoX2JhY2sobWFzWzBdIC8gaSk7CiAgICAgICAgICAgIGlmIChpKmkgPT0gbWFzWzBdKQogICAgICAgICAgICAgICAgZGVsLnBvcF9iYWNrKCk7CiAgICAgICAgfQogICAgfQogICAgc29ydChkZWwuYmVnaW4oKSwgZGVsLmVuZCgpKTsKICAgIGtvbGwucmVzaXplKGRlbC5zaXplKCkpOwogICAgZGZzMigwLCAwKTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQogICAgICAgIGNvdXQ8PGFuc1tpXTw8JyAnOwogICAgcmV0dXJuIDA7Cn0=