#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5;
const int INF = 987654321;
int TC, N, A[MAXN + 10], D;
vector<int> adj[MAXN + 10];
int dp1[MAXN + 10], dp2[MAXN + 10];
void dfs(int now, int bef) {
  int chd = 0;
  for (int nxt : adj[now]) {
    if (nxt == bef) continue;
    dfs(nxt, now);
    chd++;
  }
  if (chd == 0) {
    dp1[now] = dp2[now] = 1;
    if (A[now] == 1) dp2[now] = INF;
    if (A[now] == 2) dp1[now] = INF;
    return;
  }
  int p = 0, q = 0;
  for (int nxt : adj[now]) {
    if (nxt == bef) continue;
    int t = min(dp1[nxt], dp2[nxt] + 1);
    if (t >= p)
      q = p, p = t;
    else if (t >= q)
      q = t;
  }
  dp1[now] = p;
  if (p + q - 1 > D) dp1[now] = INF;
  p = 0, q = 0;
  for (int nxt : adj[now]) {
    if (nxt == bef) continue;
    int t = min(dp2[nxt], dp1[nxt] + 1);
    if (t >= p)
      q = p, p = t;
    else if (t >= q)
      q = t;
  }
  dp2[now] = p;
  if (p + q - 1 > D) dp2[now] = INF;
  if (A[now] == 1) dp2[now] = INF;
  if (A[now] == 2) dp1[now] = INF;
}
int main() {
  scanf("%d", &TC);
  while (TC--) {
    scanf("%d", &N);
    for (int i = 1; i <= N; i++) scanf("%d", &A[i]);
    for (int i = 1; i < N; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    int lo = 0, hi = N;
    while (lo + 1 < hi) {
      int mid = lo + hi >> 1;
      D = mid;
      dfs(1, 1);
      if (min(dp1[1], dp2[1]) <= mid)
        hi = mid;
      else
        lo = mid;
    }
    hi /= 2;
    hi++;
    printf("%d\n", hi);
    for (int i = 1; i <= N; i++) adj[i].clear();
  }
}
