Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very interesting tree rooted at vertex 1. There is an integer number written on each vertex of the tree; the number written on vertex i is equal to a_{i}.

Ilya believes that the beauty of the vertex x is the greatest common divisor of all numbers written on the vertices on the path from the root to x, including this vertex itself. In addition, Ilya can change the number in one arbitrary vertex to 0 or leave all vertices unchanged. Now for each vertex Ilya wants to know the maximum possible beauty it can have.

For each vertex the answer must be considered independently.

The beauty of the root equals to number written on it.


-----Input-----

First line contains one integer number n — the number of vertices in tree (1 ≤ n ≤ 2·10^5).

Next line contains n integer numbers a_{i} (1 ≤ i ≤ n, 1 ≤ a_{i} ≤ 2·10^5).

Each of next n - 1 lines contains two integer numbers x and y (1 ≤ x, y ≤ n, x ≠ y), which means that there is an edge (x, y) in the tree.


-----Output-----

Output n numbers separated by spaces, where i-th number equals to maximum possible beauty of vertex i.


-----Examples-----
Input
2
6 2
1 2

Output
6 6 

Input
3
6 2 3
1 2
1 3

Output
6 6 6 

Input
1
10

Output
10

Source: codeforces | Difficulty: HARD
Tags: Graph algorithms

Graph traversal

Number theory

Tree algorithms

Mathematics


Solutions:
from sys import stdin
from fractions import gcd
n = int(stdin.readline().strip())
v = list(map(int, stdin.readline().strip().split()))
adj = [[] for _ in range(n)]
for _ in range(n - 1):
	(x, y) = list(map(int, stdin.readline().strip().split()))
	adj[x - 1].append(y - 1)
	adj[y - 1].append(x - 1)
root_divisors = []
cnt = [0] * 200001
d = 1
while d * d <= v[0]:
	if v[0] % d == 0:
		root_divisors.append(d)
		cnt[d] += 1
		if v[0] // d != d:
			root_divisors.append(v[0] // d)
			cnt[v[0] // d] += 1
	d += 1
s = [0]
visited = [False] * n
visited[0] = True
level = [1] * n
res1 = [0] * n
res2 = [0] * n
res1[0] = v[0]
d = 1
while s:
	x = s[-1]
	any_more = False
	while adj[x]:
		y = adj[x].pop()
		if not visited[y]:
			visited[y] = True
			any_more = True
			s.append(y)
			level[y] = level[x] + 1
			res2[y] = gcd(res2[x], v[y])
			for d in root_divisors:
				if v[y] % d == 0:
					cnt[d] += 1
				if cnt[d] == level[y] or cnt[d] == level[y] - 1:
					res1[y] = max(res1[y], res2[y], d)
			break
	if not any_more:
		s.pop()
		for d in root_divisors:
			if v[x] % d == 0:
				cnt[d] -= 1
print(' '.join(list(map(str, res1))))