You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour.

Let's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the subtree of vertex v more times than colour c. So it's possible that two or more colours will be dominating in the subtree of some vertex.

The subtree of vertex v is the vertex v and all other vertices that contains vertex v in each path to the root.

For each vertex v find the sum of all dominating colours in the subtree of vertex v.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of vertices in the tree.

The second line contains n integers ci (1 ≤ ci ≤ n), ci — the colour of the i-th vertex.

Each of the next n - 1 lines contains two integers xj, yj (1 ≤ xj, yj ≤ n) — the edge of the tree. The first vertex is the root of the tree.

Output

Print n integers — the sums of dominating colours for each vertex.

Examples

Input

4
1 2 3 4
1 2
2 3
2 4


Output

10 9 3 4


Input

15
1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
1 2
1 3
1 4
1 14
1 15
2 5
2 6
2 7
3 8
3 9
3 10
4 11
4 12
4 13


Output

6 5 4 3 2 3 3 1 1 3 2 2 1 2 3
import os
import sys
from io import BytesIO, IOBase

_str = str
BUFSIZE = 8192


def str(x=b''):
    return x if type(x) is bytes else _str(x).encode()


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda: self.buffer.read().decode('ascii')
        self.readline = lambda: self.buffer.readline().decode('ascii')


def inp():
    return sys.stdin.readline().rstrip()


def mpint():
    return map(int, inp().split(' '))


def itg():
    return int(inp())


# ############################## import
# from ACgenerator.Y_Testing import get_code
def tree_bfs(tree, flat=True, start=0) -> list:
    stack = [start]
    result = []
    while stack:
        new_stack = []
        if flat:
            result.extend(stack)
        else:
            result.append(stack)
        for node in stack:
            new_stack.extend(tree[node])
        stack = new_stack
    return result


def to_tree(graph, root=0):
    """
    graph: [{child, ...}, ...]  (undirected)
    :return directed graph that parent -> children
    """
    stack = [[root]]
    while stack:
        if not stack[-1]:
            del stack[-1]
            continue
        parent = stack[-1].pop()
        for child in graph[parent]:
            graph[child].discard(parent)
        stack.append(list(graph[parent]))


def tree_postorder(tree, flat=True, root=0):
    """ children -> parent """
    return tree_bfs(tree, flat, root)[::-1]


# ############################## main
from collections import defaultdict


class Counter:
    def __init__(self, c):
        self.ans = c
        self.c_t = defaultdict(lambda: 0)
        self.mx = self.c_t[c] = 1
        self.t_c = defaultdict(set)
        self.t_c[1] = {c}

    def merge(self, other):
        for c, t in other.c_t.items():
            self.add(c, t)

    def add(self, c, t):
        old_t = self.c_t[c]
        self.c_t[c] = new_t = old_t + t
        self.t_c[old_t].discard(c)
        self.t_c[new_t].add(c)
        if new_t == self.mx:
            self.ans += c
        elif new_t > self.mx:
            self.mx = new_t
            self.ans = c

    def __len__(self):
        return len(self.c_t)

    def __repr__(self):
        return _str(self.ans)


def solve():
    n = itg()
    ans = list(map(Counter, mpint()))
    tree = [set() for _ in range(n)]
    for _ in range(n - 1):
        a, b = mpint()
        a -= 1
        b -= 1
        tree[a].add(b)
        tree[b].add(a)
    to_tree(tree)

    for parent in tree_postorder(tree):
        for child in tree[parent]:
            child_ans = ans[child].ans
            if len(ans[parent]) < len(ans[child]):
                ans[parent], ans[child] = ans[child], ans[parent]
            ans[parent].merge(ans[child])
            ans[child] = child_ans
    return ans


def main():
    # solve()
    print(*solve())
    # for _ in range(itg()):
    #     print(solve())
    # print("YES" if solve() else "NO")
    # print("yes" if solve() else "no")


DEBUG = 0
URL = 'https://codeforces.com/contest/600/problem/E'

if __name__ == '__main__':
    if DEBUG == 1:
        import requests  # ImportError: cannot import name 'md5' from 'sys' (unknown location)
        from ACgenerator.Y_Test_Case_Runner import TestCaseRunner

        runner = TestCaseRunner(main, URL)
        inp = runner.input_stream
        print = runner.output_stream
        runner.checking()
    elif DEBUG == 2:
        main()
    else:
        sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
        main()
# Please check!
