n=int(input()) a=list(map(int,input().split())) e=[[] for i in range(n)] for i in range(n-1): u,v=map(int,input().split()) u-=1 v-=1 e[u]+=[v] e[v]+=[u] M=998244353 def union(x,y): rx=root(x) ry=root(y) if rx==ry: return if len(f[rx])x: break c=0 while x%y==0: x//=y c+=1 if c>0: f[i][y]=c if x>0: f[i][x]=1 v=[0]*n u=[0]*n q=[0] while len(q)>0: s=q[-1] if v[s]==0: v[s]=1 q+=[t for t in e[s] if v[t]==0] else: for t in e[s]: if v[t]==0: union(s,t) rs=root(s) u[s]=a[rs] v[s]=0 q.pop() print(*u,sep="\n")