#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 7;
int root, cnt, mxLength;
vector<int> a(N), ok(N), dis1(N), dis2(N), g[N];
void dfs(int u, int p){
for (auto v : g[u]){
if (v == p) continue;
dfs(v, u);
if (!root && a[v]) root = v;
}
}
void dfs2(int u, int p){
for (auto v : g[u]){
if (v == p) continue;
dfs2(v, u);
a[u] = max(a[u], a[v]);
}
}
void dfs3(int u, int cur){
cnt++;
a[u] = 0;
dis1[u] = cur;
for (auto v : g[u]){
if (a[v]){
dfs3(v, cur + 1);
ok[u]++;
if (u > 0 && ok[u] > 1) mxLength = max(mxLength, dis2[u] + dis2[v] + 1);
mxLength = max(mxLength, dis1[v] + dis2[v]);
dis2[u] = max(dis2[u], dis2[v] + 1);
}
}
}
int main(){
int t;
cin >> t;
while (t--){
int n, u, v;
cin >> n;
for (int i = 1; i <= n; i++){
cin >> a[i];
ok[i] = dis1[i] = dis2[i] = 0;
g[i].clear();
}
for (int i = 1; i < n; i++){
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
dfs2(root, 0); //cout<<root<<endl;
dfs3(root, 0);
cout << (cnt - 1) * 2 - mxLength << endl;
root = mxLength = cnt = ok[0] = 0;
// for (int i = 1; i <= n; i++) cout<<dis1[i]<<" ";cout<<endl;
// for(int i=1;i<=n; i++)cout<<dis2[i]<<" ";cout<<endl;
}
}