#include<bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define all(v) (v).begin(), (v).end()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define F first
#define S second
const ll M = 1e18 + 7;
const int N = 2e5 + 7;
const int MOD = 1e9 + 7;
int last_ver = 0;
int root = 0;
void root_find(int node,vector<int> &vis, vector<int> check, vector<int> adj[]){
vis[node] = 1;
for (auto it : adj[node]){
if(!vis[it]) {
root_find(it, vis, check, adj);
if (!root && check[it]) root = it;
}
}
}
int dfs(int node, int cost, vector<int> &vis, vector<int> check, vector<int> &depth, vector<int> adj[]) {
vis[node] = 1;
if(check[node]) last_ver = node;
int tmp = 0;
for(auto it: adj[node]) {
if(!vis[it]) {
depth[it] = depth[node] + 1;
tmp += dfs(it,2,vis,check,depth,adj);
}
}
if(tmp == 0 && !check[node]) return 0;
return tmp + cost;
}
void Hrid_solve(){
int n;
cin >> n;
vector<int> check(n), vis(n,0), depth(n,0);
for(int i = 0; i < n; i++) {
cin >> check[i];
if(check[i]) root = i;
}
vector<int> adj[n];
for(int i = 0; i < n-1; i++) {
int u,v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
if(count(all(check),1)==0) {
cout << 0 << endl;
return;
}
int tmp = adj[0].size();
int mx = 0;
root_find(0,vis,check,adj);
for(int i = 0; i < n; i++) {
// if(check[i]) {
// if(adj[i].size() <= tmp) root = i;
// }
vis[i] = 0;
}
int ans = dfs(root,0,vis,check,depth,adj);
for(int i = 0; i < n; i++) {
if(check[i]) {
mx = max(mx, depth[i]);
}
}
// cout << last_ver << endl;
// cout << ans << " ";
cout << ans-mx << endl;
root = 0;
}
/* ************** What defines us is how well we rise after we Fall !! ************** */
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tc = 1;
cin >> tc;
for(int kase = 1; kase <= tc; kase++){
// cout << "Case " << kase << ": ";
Hrid_solve();
}
return 0;
}