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 java.util.*;
import java.io.*;
public class E {
	static long[] sum;
	static int[] color;
	static ArrayList[] children;
	public static void main(String[] args) throws Exception {
		Parser sc = new Parser(System.in);
		int N = sc.nextInt();
		sum = new long[N];
		color = new int[N];
		children = new ArrayList[N];
		for (int i = 0; i < N; i++) {
			children[i] = new ArrayList();
			color[i] = sc.nextInt();
		}
		for (int i = 0; i < N-1; i++) {
			int from = sc.nextInt()-1;
			int to = sc.nextInt()-1;
			children[from].add(to);
			children[to].add(from);
		}
		recurse(0, -1);
		StringBuilder sb = new StringBuilder("");
		sb.append(sum[0]);
		for (int i = 1; i < N; i++) {
			sb.append(" ");
			sb.append(sum[i]);
		}	
		System.out.println(sb.toString());
	}
	
	static TreeRet recurse(int curNode, int prev) {
		TreeRet retval = new TreeRet();
		retval.colorFreq.put(color[curNode],1);
		retval.sum = color[curNode];
		retval.maxCount = 1;
		for (int i = 0; i < children[curNode].size(); i++) {
			int adj = (Integer) children[curNode].get(i);
			if (adj == prev) continue;
			TreeRet childVals = recurse(adj, curNode);
			retval = merge(retval, childVals);
		}
		sum[curNode] = retval.sum;
		return retval;
	}
	
	static TreeRet merge(TreeRet s1, TreeRet s2) {
		if (s1.colorFreq.size() > s2.colorFreq.size()) {
			TreeRet temp = s1;
			s1 = s2;
			s2 = temp;
		}
		//s1 small, s2 large
		for (Map.Entry<Integer, Integer> ent : s1.colorFreq.entrySet()) {
			int color = ent.getKey();
			int amount = ent.getValue();
			int largeAmt = 0;
			if (s2.colorFreq.containsKey(color)) largeAmt = s2.colorFreq.get(color);
			amount = amount+largeAmt;
			if (amount == s2.maxCount) {
				s2.sum += (1l*color);
			} else if (amount > s2.maxCount) {
				s2.maxCount = amount;
				s2.sum = (1l*color);
			}
			s2.colorFreq.put(color, amount);
		}
		return s2;
	}
}
class TreeRet {
	HashMap<Integer, Integer> colorFreq = new HashMap<Integer, Integer>();
	long sum;
	int maxCount;
}
class Parser
{
   final private int BUFFER_SIZE = 1 << 16;

   private DataInputStream din;
   private byte[] buffer;
   private int bufferPointer, bytesRead;

   public Parser(InputStream in)
   {
      din = new DataInputStream(in);
      buffer = new byte[BUFFER_SIZE];
      bufferPointer = bytesRead = 0;
   }

   public long nextLong() throws Exception
   {
      long ret = 0;
      byte c = read();
      while (c <= ' ') c = read();
      boolean neg = c == '-';
      if (neg) c = read();
      do
      {
         ret = ret * 10 + c - '0';
         c = read();
      } while (c > ' ');
      if (neg) return -ret;
      return ret;
   }
   
   //reads in the next string
   public String next() throws Exception
   {
      StringBuilder ret =  new StringBuilder();
      byte c = read();
      while (c <= ' ') c = read();
      do
      {
         ret = ret.append((char)c);
         c = read();
      } while (c > ' ');
      return ret.toString();
   }

   public int nextInt() throws Exception
   {
      int ret = 0;
      byte c = read();
      while (c <= ' ') c = read();
      boolean neg = c == '-';
      if (neg) c = read();
      do
      {
         ret = ret * 10 + c - '0';
         c = read();
      } while (c > ' ');
      if (neg) return -ret;
      return ret;
   }
   
   private void fillBuffer() throws Exception
   {
      bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
      if (bytesRead == -1) buffer[0] = -1;
   }

   private byte read() throws Exception
   {
      if (bufferPointer == bytesRead) fillBuffer();
      return buffer[bufferPointer++];
   }
}