#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
inline void amin(T &x, const T &y) {
  if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
  if (x < y) x = y;
}
int N;
vector<int> G[200011];
int A[200011];
int par[200001];
vector<int> que;
int dpB[200011];
int dpW[200011];
void MAIN() {
  scanf("%d", &N);
  for (int i = 0, i_len = (N); i < i_len; ++i) G[i].clear();
  for (int i = 0, i_len = (N); i < i_len; ++i) scanf("%d", A + i);
  for (int i = 0, i_len = (N - 1); i < i_len; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  que.clear();
  que.push_back(0);
  for (int i = 0, i_len = (N); i < i_len; ++i) {
    int v = que[i];
    for (__typeof((G[v]).begin()) e = (G[v]).begin(), e_end = (G[v]).end();
         e != e_end; ++e)
      if (*e != par[v]) {
        par[*e] = v;
        que.push_back(*e);
      }
  }
  int ans = 0;
  for (int i = 0, i_len = (N); i < i_len; ++i) {
    int v = que[N - 1 - i];
    int bb[3] = {0, 0, 0};
    int ww[3] = {0, 0, 0};
    for (__typeof((G[v]).begin()) e = (G[v]).begin(), e_end = (G[v]).end();
         e != e_end; ++e)
      if (*e != par[v]) {
        bb[0] = dpB[*e];
        ww[0] = dpW[*e];
        sort(bb, bb + 3);
        sort(ww, ww + 3);
      }
    amax(ans, ww[2] + bb[2]);
    if (A[v] == 0) {
      amax(ans, bb[1] - 1 + bb[2]);
      amax(ans, ww[1] - 1 + ww[2]);
      dpW[v] = ww[2];
      dpB[v] = bb[2];
    }
    if (A[v] == 1) {
      amax(ans, bb[1] + 1 + bb[2]);
      amax(ans, ww[1] - 1 + ww[2]);
      dpW[v] = max(ww[2], 1 + bb[2]);
      dpB[v] = 0;
    }
    if (A[v] == 2) {
      amax(ans, ww[1] + 1 + ww[2]);
      amax(ans, bb[1] - 1 + bb[2]);
      dpW[v] = 0;
      dpB[v] = max(bb[2], 1 + ww[2]);
    }
  }
  ans = (ans + 2) / 2;
  printf("%d\n", ans);
}
int main() {
  int TC = 1;
  scanf("%d", &TC);
  for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN();
  return 0;
}
