H: Colorful Tree

Story

Yamiuchi (assassination) is a traditional event that is held annually in JAG summer camp. Every team displays a decorated tree in the dark and all teams' trees are compared from the point of view of their colorfulness. In this competition, it is allowed to cut the other teams’ tree to reduce its colorfulness. Despite a team would get a penalty if it were discovered that the team cuts the tree of another team, many teams do this obstruction.

You decided to compete in Yamiuchi and write a program that maximizes the colorfulness of your team’s tree. The program has to calculate maximum scores for all subtrees in case the other teams cut your tree.

Problem Statement

You are given a rooted tree G with N vertices indexed with 1 through N. The root is vertex 1. There are K kinds of colors indexed with 1 through K. You can paint vertex i with either color c_i or d_i. Note that c_i = d_i may hold, and if so you have to paint vertex i with c_i (=d_i).

Let the colorfulness of tree T be the number of different colors in T. Your task is to write a program that calculates maximum colorfulness for all rooted subtrees. Note that coloring for each rooted subtree is done independently, so previous coloring does not affect to other coloring.

Input


N K
u_1 v_1
:
u_{N-1} v_{N-1}
c_1 d_1
:
c_N d_N


The first line contains two integers N and K in this order.

The following N-1 lines provide information about the edges of G. The i-th line of them contains two integers u_i and v_i, meaning these two vertices are connected with an edge.

The following N lines provide information about color constraints. The i-th line of them contains two integers c_i and d_i explained above.

Constraints

* 1 \leq N \leq 10^5
* 1 \leq K \leq 2\times 10^5
* 1 \leq u_i , v_i \leq N
* 1 \leq c_i , d_i \leq K
* The input graph is a tree.
* All inputs are integers.



Output

Output N lines.

The i-th line of them contains the maximum colorfulness of the rooted subtree of G whose root is i.

Sample Input 1


2 10
1 2
1 9
8 7


Output for Sample Input 1


2
1


Sample Input 2


3 2
1 2
1 3
1 2
1 1
1 2


Output for Sample Input 2


2
1
1


Note that two color options of a vertex can be the same.

Sample Input 3


5 100000
4 3
3 5
1 3
2 1
3 2
1 3
2 1
4 2
1 4


Output for Sample Input 3


4
1
3
1
1






Example

Input

2 10
1 2
1 9
8 7


Output

2
1
#include<bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template <typename T> bool chkmax(T &x,T y){return x<y?x=y,true:false;}
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,true:false;}

int readint(){
	int x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

int n,m,tot,now;
int v[200005],nxt[200005],h[100005],siz[100005],f[200005],son[100005],col[200005],a[100005],b[100005],ans[100005];
bool vis[200005];
vector<int> gar;

int getf(int x){return x==f[x]?x:f[x]=getf(f[x]);}

void addedge(int x,int y){
	v[++tot]=y; nxt[tot]=h[x]; h[x]=tot;
	v[++tot]=x; nxt[tot]=h[y]; h[y]=tot;
}

void dfs1(int u,int fa){
	siz[u]=1;
	for(int p=h[u];p;p=nxt[p]){
		if(v[p]==fa) continue;
		dfs1(v[p],u);
		if(siz[v[p]]>siz[son[u]]) son[u]=v[p];
		siz[u]+=siz[v[p]];
	}
}

void change(int x,int y){
	if(!col[x]) gar.pb(x),col[x]=1;
	if(!col[y]) gar.pb(y),col[y]=1;
	int fx=getf(x),fy=getf(y);
	if(fx==fy){
		if(!vis[fx]) now++,vis[fx]=1;
		return;
	}
	f[fx]=fy;
	if(!vis[fx]&&!vis[fy]){
		now++;
		return;
	}
	if(vis[fx]&&vis[fy]) return;
	vis[fy]=1,now++;
}

void add(int u,int fa){
	change(a[u],b[u]);
	for(int p=h[u];p;p=nxt[p]){
		if(v[p]==fa) continue;
		add(v[p],u);
	}
}

void dfs2(int u,int fa,bool kp){
	for(int p=h[u];p;p=nxt[p]) if(v[p]!=fa&&v[p]!=son[u]) dfs2(v[p],u,0);
	if(son[u]) dfs2(son[u],u,1);
	for(int p=h[u];p;p=nxt[p]) if(v[p]!=fa&&v[p]!=son[u]) add(v[p],u);
	change(a[u],b[u]);
	ans[u]=now;
	if(!kp){
		for(auto x:gar) f[x]=x,vis[x]=0,col[x]=0;
		gar.clear();
		now=0;
	}
}

int main(){
	n=readint(); m=readint();
	for(int i=1;i<=m;i++) f[i]=i;
	for(int i=1;i<n;i++) addedge(readint(),readint());
	for(int i=1;i<=n;i++) a[i]=readint(),b[i]=readint();
	dfs1(1,-1);
	dfs2(1,-1,1);
	for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
	return 0;
}
