#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int T, n, d[maxn], col[maxn];
vector<int> G[maxn];
queue<int> q[3];
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      G[i].clear(), scanf("%d", &col[i]);
    }
    for (int i = 1, u, v; i < n; i++) {
      scanf("%d %d", &u, &v);
      G[u].push_back(v), G[v].push_back(u);
    }
    if (n == 1) {
      printf("1\n");
      continue;
    }
    int ans = INT_MAX;
    for (int c : {1, 2}) {
      for (int i = 1; i <= n; i++) {
        d[i] = G[i].size();
        if (d[i] == 1) q[col[i]].push(i);
      }
      int cnt = 0;
      while (!q[0].empty() || !q[1].empty() || !q[2].empty()) {
        cnt++;
        while (!q[0].empty() || !q[c].empty()) {
          if (!q[0].empty()) {
            int v = q[0].front();
            q[0].pop();
            for (int u : G[v]) {
              if (--d[u] == 1) q[col[u]].push(u);
            }
          } else {
            int v = q[c].front();
            q[c].pop();
            for (int u : G[v]) {
              if (--d[u] == 1) q[col[u]].push(u);
            }
          }
        }
        c = 3 - c;
      }
      ans = min(ans, cnt);
    }
    printf("%d\n", ans);
  }
  return 0;
}
