#include<bits/stdc++.h> // --> Bismillahir_ Rahmanir_ Rahim <--
#define ll long long
using namespace std;
const ll N = 3e5;
vector<ll>g[N];
ll n,e;
ll a[N];
bool vis[N];
ll z = 0,ans = 0;
void dfs(ll u) {
vis[u] = true;
if(a[u]==1) {
ans += z;
z = 0;
}
for(auto &v : g[u]) {
if(!vis[v]) {
++z;
dfs(v);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll tc; cin >> tc;
while(tc--) {
cin >> n;
e = n-1;
ll st = -1, en = -1;
for(ll i=1 ; i<=n ; i++) {
cin >> a[i];
if(a[i]==1) en = i;
if(a[i]==1 && st==-1) st = i;
}
while(e--) {
ll x,y; cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
if(st==-1) {
cout << 0 << "\n";
}
else {
ll strt = (st+en)/2;
z = 0;
ans = 0;
dfs(strt);
cout << ans << "\n";
}
for(ll i=0 ; i<N ; i++) {
g[i].clear();
vis[i] = 0;
}
}
return 0;
}