#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define endme "\n";
#define ffprint(x) \
for (auto &i : x) \
{ \
cout << i << " "; \
} \
cout<<endme;
#define vectinput(x) for(auto &i : x) cin >> i;
#ifndef ONLINE_JUDGE
#include"Debug.cpp"
#endif
class DSUrank{
public:
vector<int>parent;
vector<int>rank;
DSUrank(int n){
parent.resize(n);
rank.resize(n);
for(int i=0;i<n;i++)parent[i]=i;
}
int find_parent(int x){
if(parent[x]==x)return x;
parent[x]=find_parent(parent[x]);
return parent[x];
}
void union_(int x,int y){
int pary=find_parent(y);
int parx=find_parent(x);
if(pary==parx)return;
if(rank[parx]>rank[pary]){
parent[pary]=parx;
}else if(rank[parx]<rank[pary]){
parent[parx]=pary;
}else{
parent[parx]=pary;
rank[pary]++;
}
}
};
void solve(){
int n;
cin>>n;
vector<pair<int,int>>edges(n-1);
vector<int>color(n);
for(auto &i:edges){
cin>>i.first>>i.second;
i.first--;
i.second--;
}
vectinput(color);
DSUrank mydsu(n);
for(auto &i:edges){
if(color[i.first]==color[i.second]){
mydsu.union_(i.first,i.second);
}
}
map<int,int> mp;
for(int i=0;i<n;i++){
int x=mydsu.find_parent(i);
mp[x]++;
}
int answer=0;
for(auto &i:mp){
answer+=((i.second-1)*(i.second-2))/2;
}
cout<<answer<<endme;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("0-InputFile/input.txt", "r", stdin);
freopen("0-InputFile/output.txt", "w", stdout);
#endif
int t;
cin>>t;
// t=1;
while (t--) {
solve();
}
return 0;
}
/*
reset_template.bat
%%%%%%%%+++*%@@@@@%%@%%%%%%%%%%%@@%%%%%%##**###%%%
%%%%%%%%#*#%@@@@@@@%%%%%#%%%@@@@@%####**####%%@@@@
%%%%%%%%##%@@@@@@@*+++++++++*#@@@@%####%%%%@@@@@@@
%%%%%%%%#%%@@@@@#++++++++++++@@@@@@@%%%@@@@@@@@@@@
@%#%%#%#%%%%%%*++++++++++++++*@@@@@@#++++++*######
%%%%%#%%%###++++++++++++++++++*#@@@#*******#******
#%%%%%%%%%%*+++++******+++++++++**++*##**+++***##%
%%#%%%%%@@*++++*%@@#*****#%#*++++++++++++**##%@@@%
%%%%%%%%%#+***#@@@#++*++*@@@#***+++++++*#%@@%##%%%
%%%%%%%%#**###%@@#+++++++#@@@#*#**++**##%#####%@@@
%%%%%%%%%**####%#++*##*++*@@@%###***#@@@@@%%%%@@@@
%%%%%%%%%#*#####**#@@@@@***%%%%%##*%@@@@@@@@%%@@%%
%%%%%%%%#%**######%%@@%##%%%%%%%##%@@@@@@@@@%#****
%@@%%%###@@%#%%%@%%@@@@@@@%%%%%%%@@@@@@@@@@@@%#**#
%%%%%%%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@###
%%%#%%%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%%%%%%%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%
%%%%%%%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%
*/