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
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
template <typename T>
inline T read() {
  T sum = 0, fl = 1;
  int ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') fl = -1;
  for (; isdigit(ch); ch = getchar()) sum = sum * 10 + ch - '0';
  return sum * fl;
}
const int N = 1e5 + 10;
int cnt, head[N], to[N << 1], nxt[N << 1], n, arr[N];
int check[N], sz[N], son[N];
void addedge(int a, int b) {
  to[++cnt] = b;
  nxt[cnt] = head[a];
  head[a] = cnt;
}
void dfs1(int x, int y) {
  sz[x] = 1;
  for (int i = head[x]; i; i = nxt[i]) {
    int cur = to[i];
    if (sz[cur]) continue;
    dfs1(cur, x);
    sz[x] += sz[cur];
  }
}
bool big[N];
long long int ans[N], can, maxx;
void reval(int x) {
  if (maxx < ++check[x])
    maxx = check[x], can = x;
  else if (maxx == check[x])
    can += x;
}
void add(int a, int b) {
  reval(arr[a]);
  for (int i = head[a]; i; i = nxt[i]) {
    if (to[i] != b && !big[to[i]]) add(to[i], a);
  }
}
void remover(int x, int y) {
  check[arr[x]]--;
  for (int i = head[x]; i; i = nxt[i]) {
    if (to[i] != y && !big[to[i]]) remover(to[i], x);
  }
}
void helper(int x, int y, int keep) {
  int mx = 1, bigChild = -1;
  for (int i = head[x]; i; i = nxt[i]) {
    if (to[i] != y && sz[to[i]] > mx) mx = sz[to[i]], bigChild = to[i];
  }
  for (int i = head[x]; i; i = nxt[i]) {
    if (to[i] != y && to[i] != bigChild) helper(to[i], x, 0);
  }
  if (bigChild != -1) helper(bigChild, x, 1), big[bigChild] = 1;
  add(x, y);
  ans[x] = can;
  if (~bigChild) big[bigChild] = 0;
  if (!keep) remover(x, y), maxx = can = 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    addedge(a, b);
    addedge(b, a);
  }
  dfs1(1, 0);
  helper(1, 0, 1);
  for (int i = 1; i <= n; i++) printf("%lld\n", ans[i]);
  return 0;
}
