#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 2e5 + 10;
const ll MOD = 1e9 + 7;
int root, cnt, mxlen;
vector<int> v(N), dist(N), g[N];
void dfs(int vertex, int par){
for (auto child : g[vertex]){
if (child == par) continue;
dfs(child, vertex);
v[vertex] = max(v[vertex], v[child]);
}
}
void dfs2(int vertex, int par, int cur) {
mxlen = max(mxlen, cur);
cnt++;
dist[vertex] = cur;
for(auto child : g[vertex]) {
if (child == par) continue;
if(v[child]) {
dfs2(child, vertex, cur + 1);
}
}
}
void solve()
{
for(int i = 0; i < N; i++) {
g[i].clear();
dist[i] = 0;
v[i] = 0;
root = mxlen = cnt = 0;
}
int n;
cin >> n;
int root = 0;
for(int i = 1; i <= n; i++) {
cin >> v[i];
if(v[i] == 1) {
root = i;
}
}
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(root, 0);
dfs2(root, 0, 0);
int ans = (cnt - 1) * 2, mx = 0;
for(int i = 1; i <= n; i++) {
if(dist[i] > mx) {
root = i;
mx = dist[i];
}
}
dfs2(root, 0, 0);
ans -= mxlen;
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int t;
cin >> t;
while(t--){
solve();
}
}