#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
vector<int>adj[N];
int cnt = 0;
vector<bool>vis(N, false);
void dfs(int node, vector<int>&apple, int total){
vis[node] = true;
for(auto child : adj[node]){
if(!vis[child]){
if(apple[child] == 1){
total--;
cnt++;
//cout << child <<" " << total << '\n';
if(total == 0)break;
dfs(child, apple, total);
}
else {
cnt++;
//cout << child << '\n';
dfs(child, apple, total);
}
if(total == 0){
return;
}
}
}
}
void solve(){
for(int i = 0; i < N; i++){
vis[i] = false;
adj[i].clear();
}
cnt = 0;
int n;
cin >> n;
vector<int>apple(n);
int start = 0, total = 0;
for(int i = 0; i < n; i++){
cin >> apple[i];
if(apple[i] == 1){
total++;
}
}
--total;
if(total == 0){
cout << 0 << '\n';
return;
}
for(int i = 0; i < n; i++){
if(apple[i] == 1){
start = i + 1;
break;
}
}
//cout << start << '\n';
for(int i = 0; i < n - 1; i++){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(start, apple, total);
cout << cnt << '\n';
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
cin >> t;
while(t--){
solve();
}
return 0;
}