#include <bits/stdc++.h>
using namespace std;
int n, m;
int A[200036];
vector<int> G[200036];
int dis[200036], as;
void dfs(int u, int f, int wf) {
  if (!as || dis[u] > dis[as]) as = u;
  for (int v : G[u])
    if (v != f) {
      dis[v] = dis[u] + (wf != A[v] && A[v]);
      dfs(v, u, A[v] ? A[v] : wf);
    }
}
void solve() {
  scanf("%d", &n);
  for (int i = (1), iend = (n); i <= iend; ++i) G[i].clear();
  int pr = 0;
  for (int i = (1), iend = (n); i <= iend; ++i) {
    scanf("%d", A + i);
    if (A[i]) pr = i;
  }
  for (int i = (2), iend = (n); i <= iend; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v), G[v].push_back(u);
  }
  if (!pr) return void(puts("1"));
  dis[pr] = 1, as = 0, dfs(pr, pr, A[pr]);
  int s = as;
  dis[s] = 1, as = 0, dfs(s, s, A[s]);
  printf("%d\n", (dis[as] >> 1) + 1);
}
signed main() {
  int T;
  cin >> T;
  while (T--) solve();
}
