#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 root;
vector<int> adj[N];
int depth[N];
vector<int> check(N);
// int dfs(int node, int cost, int par) {
// int tmp = 0;
// for(auto it: adj[node]) {
// if(it == par) continue;
// depth[it] = depth[node] + 1;
// tmp += dfs(it,2,node);
// }
// if(tmp == 0 && !check[node]) return 0;
// return tmp + cost;
// }
void dfs2(int node, int par) {
for(auto it: adj[node]) {
if(it == par) continue;
depth[it] = depth[node]+1;
dfs2(it,node);
check[node] = max(check[node],check[it]);
}
}
void dfs3(int node, int par) {
for(auto it: adj[node]) {
if(it == par) continue;
depth[it] = depth[node]+1;
dfs3(it,node);
}
}
void Hrid_solve(){
int n;
cin >> n;
bool f = true;
for(int i = 1; i <= n; i++) {
cin >> check[i];
if(check[i]) f = false;
}
for(int i = 0; i < n-1; i++) {
int u,v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
if(f) {
cout << 0 << endl;
return;
}
int total = 0;
int root = 0;
int mx = -1;
for(int i = 1; i <= n; i++) {
if(check[i]) {
// total = dfs(i,0,-1);
dfs2(i,-1);
root = i;
break;
}
}
for(int i = 1; i <= n; i++) {
// cout << i << " " << check[i] << endl;
if(check[i]) {
if(depth[i] > mx) {
mx = depth[i];
root = i;
}
total++;
}
depth[i] = 0;
}
total = (total - 1) * 2;
dfs3(root,-1);
mx = -1;
for(int i = 1; i <= n; i++) {
if(check[i]) {
if(depth[i] > mx) {
mx = depth[i];
}
}
depth[i] = 0;
adj[i].clear();
check[i] = 0;
}
// cout << mx << endl;
cout << total-mx << endl;
}
/* ************** 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;
}