#define _GLIBCXX_FILESYSTEM
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5+2;
ll n,ans[N],root,mx;
bool a[N],flag[N];
vector<int> g[N];
void dfs(int u,int p = 0) {
if(a[u] == 1) {
flag[u] = 1;
}
bool f = 0;
for(auto x: g[u]) {
if(x == p) continue;
dfs(x,u);
if(flag[x]) f = 1;
flag[u] |= flag[x];
}
// if(u == 3) cerr << f << ' ' << flag[u] << '\n';
if(!f and flag[u] and !root) {
root = u;
}
}
void dfs1(int u,int d = 0,int p = 0) {
if(a[u] == 1) {
flag[u] = 1;
mx = max(mx,(ll)d);
}
for(auto x: g[u]) {
if(x == p) continue;
dfs1(x,d+1,u);
if(flag[x])
ans[u] += ans[x]+2;
flag[u] |= flag[x];
}
}
void solve() {
cin >> n;
root = 0;
for(int i = 0; i < n; i++) {
cin >> a[i+1];
flag[i+1] = 0;
ans[i+1] = 0;
g[i+1].clear();
}
for(int i = 1; i < n; i++) {
int u,v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
// cerr << root << ' ';
if(root) {
mx = 0;
for(int i = 0; i < n; i++) flag[i+1] = 0;
dfs1(root);
ans[root] -= mx;
cout << ans[root] << '\n';
}
else cout << 0 << '\n';
return;
}
int32_t main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);
int tc = 1;
cin >> tc;
for(int kase = 1; kase <= tc; kase++) {
//cout << "Case " << kase << ": ";
solve();
}
return 0;
}