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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

public class lomsatgelral {
	//let's merge small to large
	public static void main(String[] args) {
		FastScanner scan=new FastScanner();
		PrintWriter out=new PrintWriter(System.out);
		int n=scan.nextInt();
		color=new int[n];
		for(int i=0;i<n;i++) color[i]=scan.nextInt();
		
		a=new ArrayList[n];
		for(int i=0;i<n;i++) a[i]=new ArrayList<>();
		for(int i=0;i<n-1;i++) {
			int u=scan.nextInt()-1, v=scan.nextInt()-1;
			a[u].add(v);
			a[v].add(u);
		}
		res=new long[n];
		dfs(0,-1);
		
		for(long i:res) out.print(i+" ");
		out.close();
	}
	static class node {
		int id;
		int color;//color of node
		long sum;//sum of dominating colors in the subtree
		long max;//max times any color appears in the subtree
		HashMap<Long,Long> freq;//hashmap of (color-->frequency) for subtree
		
		node() {
			sum=0;
			max=0;
			freq=new HashMap<>();
		}
		node merge(node move) {
			for(long color:move.freq.keySet()) {
				add(color,move.freq.get(color));
			}
			return this;
		}
		void add(long color, long f) {
			long add=0;
			if(freq.containsKey(color)) add=freq.get(color);
			freq.put(color,add+f);
			
			if(add+f==max) {
				sum+=color;
			}
			else if(add+f>max) {
				sum=color;
				max=add+f;
			}
		}
	}
	static ArrayList<Integer>[] a;
	static int[] color;
	static long[] res;
	
	public static node dfs(int at, int prev) {
		node cur=new node();
		cur.add(color[at],1);
		
		for(int nxt:a[at]) {
			if(nxt!=prev) {
				node nx=dfs(nxt,at);
				if(nx.freq.size()>cur.freq.size()) cur=nx.merge(cur);
				else cur=cur.merge(nx);
			}
		}
		
		res[at]=cur.sum;
		return cur;
	}
	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner() {
			try	{
				br = new BufferedReader(new InputStreamReader(System.in));
				st = new StringTokenizer(br.readLine());
			} catch (Exception e){e.printStackTrace();}
		}

		public String next() {
			if (st.hasMoreTokens())	return st.nextToken();
			try {st = new StringTokenizer(br.readLine());}
			catch (Exception e) {e.printStackTrace();}
			return st.nextToken();
		}

		public int nextInt() {return Integer.parseInt(next());}

		public long nextLong() {return Long.parseLong(next());}

		public double nextDouble() {return Double.parseDouble(next());}

		public String nextLine() {
			String line = "";
			if(st.hasMoreTokens()) line = st.nextToken();
			else try {return br.readLine();}catch(IOException e){e.printStackTrace();}
			while(st.hasMoreTokens()) line += " "+st.nextToken();
			return line;
		}
	}
}