#include<bits/stdc++.h>
#define f first
#define s second
#define pii pair<int,int>
using namespace std;
const int N = 4e6 + 5;
int t, tmin[N],tmout[N],mx[N],mn[N],timer,ans,n,f[N];
pii dp[N];
vector<int> V[N];
void dfs1(int u,int p) {
tmin[u] = ++timer;
if(!mn[f[u]]) mn[f[u]] = timer;
mx[f[u]] = timer;
for(int i = 0; i < V[u].size(); i++) {
if(V[u][i] == p) continue;
dfs1(V[u][i],u);
}
tmout[u] = timer;
}
void dfs2(int u,int p) {
dp[u].f = mn[f[u]];
dp[u].s = mx[f[u]];
for(int i = 0; i < V[u].size(); i++) {
if(V[u][i] == p) continue;
dfs2(V[u][i],u);
dp[u].f = min(dp[V[u][i]].f,dp[u].f);
dp[u].s = max(dp[V[u][i]].s,dp[u].s);
}
if(dp[u].f < tmin[u] || dp[u].s > tmout[u] || u == 1) return;
ans++;
}
signed main(){
//freopen("chainblock_input.txt","r",stdin);
//freopen("ans.txt","w",stdout);
scanf("%d",&t);
int T = 0;
while(t--){
scanf("%d",&n);
for(int i = 1; i <= n; i++) V[i].clear(), mn[i] = 0;
timer = 0; ans = 0;
for(int i = 1; i < n; i++){
int u,v;
scanf("%d",&u);
scanf("%d",&v);
V[u].push_back(v); V[v].push_back(u);
}
for(int i = 1; i <= n; i++) scanf("%d",&f[i]);
dfs1(1,0);
dfs2(1,0);
cout << "Case #"<<++T<<": "<<ans<<endl;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBmIGZpcnN0CiNkZWZpbmUgcyBzZWNvbmQKI2RlZmluZSBwaWkgcGFpcjxpbnQsaW50Pgp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjb25zdCBpbnQgTiA9IDRlNiArIDU7CmludCB0LCB0bWluW05dLHRtb3V0W05dLG14W05dLG1uW05dLHRpbWVyLGFucyxuLGZbTl07CnBpaSBkcFtOXTsKdmVjdG9yPGludD4gVltOXTsKdm9pZCBkZnMxKGludCB1LGludCBwKSB7Cgl0bWluW3VdID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