#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9, N = 5e5;
int dp[N][2], len[N][2], fst[N], nxt[N << 1], to[N << 1], mm, col[N], n, T, ans;
void ade(int u, int v) { to[++mm] = v, nxt[mm] = fst[u], fst[u] = mm; }
void dfs(int u, int fa) {
  if (col[u] == 1)
    len[u][0] = 1, len[u][1] = INF, dp[u][0] = 1, dp[u][1] = INF;
  else if (col[u] == 2)
    len[u][1] = 1, len[u][0] = INF, dp[u][1] = 1, dp[u][0] = INF;
  else
    len[u][0] = len[u][1] = dp[u][0] = dp[u][1] = 1;
  for (int i = fst[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == fa) continue;
    dfs(v, u);
    dp[u][0] = max(dp[u][0], len[u][0] + min(len[v][1], len[v][0] - 1));
    dp[u][1] = max(dp[u][1], len[u][1] + min(len[v][0], len[v][1] - 1));
    len[u][0] = max(len[u][0], min(len[v][1] + 1, len[v][0]));
    len[u][1] = max(len[u][1], min(len[v][0] + 1, len[v][1]));
  }
  ans = max(ans, min(dp[u][0], dp[u][1]));
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) fst[i] = 0;
    mm = 0;
    for (int i = 1; i <= n; i++) scanf("%d", col + i);
    for (int i = 1, u, v; i < n; i++)
      scanf("%d%d", &u, &v), ade(u, v), ade(v, u);
    ans = 0;
    dfs(1, 0);
    cout << ans / 2 + 1 << endl;
  }
}
