#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int T, n;
int a[maxn], deg[maxn];
vector<int> edge[maxn], ver[3];
bool in[maxn];
void erase(int x) {
  for (int i = 0; i < int(edge[x].size()); i++) {
    int y = edge[x][i];
    deg[y]--;
    if (deg[y] <= 1 && !in[y]) ver[a[y]].push_back(y), in[y] = true;
  }
}
int solve(int f) {
  ver[0].clear();
  ver[1].clear();
  ver[2].clear();
  int tot = 0;
  for (int i = 1; i <= n; i++) {
    deg[i] = int(edge[i].size());
    if (deg[i] <= 1)
      ver[a[i]].push_back(i), in[i] = true;
    else
      in[i] = false;
  }
  for (int t = 1;; t++) {
    while (!ver[0].empty() || !ver[f].empty()) {
      int x;
      if (!ver[0].empty())
        x = ver[0].back(), ver[0].pop_back();
      else
        x = ver[f].back(), ver[f].pop_back();
      erase(x);
      tot++;
    }
    if (tot == n) return t;
    f = 3 ^ f;
  }
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) edge[i].clear();
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n - 1; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      edge[u].push_back(v);
      edge[v].push_back(u);
    }
    printf("%d\n", min(solve(1), solve(2)));
  }
  return 0;
}
