#include <bits/stdc++.h>
using namespace std;
const int inf = 1010000000;
const int mxn = 400010;
int n, a[mxn];
int hd[mxn], nx[mxn], v[mxn], e;
int du[mxn], vis[mxn];
int o;
void init() {
  e = 0;
  for (int i = 1; i <= n; i++) hd[i] = 0;
}
void ade(int x, int y) {
  nx[++e] = hd[x];
  hd[x] = e;
  v[e] = y;
}
int ans;
queue<int> q[2];
void gg() {
  for (int i = 0; i <= n; i++) {
    du[i] = vis[i] = 0;
  }
  for (int x = 1; x <= n; x++) {
    for (int i = hd[x]; i; i = nx[i]) {
      du[x]++;
    }
  }
  while (!q[0].empty()) q[0].pop();
  while (!q[1].empty()) q[1].pop();
  for (int i = 1; i <= n; i++) {
    if (du[i] <= 1) {
      if (a[i] < 2)
        q[a[i]].push(i);
      else
        q[o].push(i);
    }
  }
  int cnt = 0, t = n, p = o;
  while (t > 0) {
    while (!q[p].empty()) {
      int x = q[p].front();
      q[p].pop();
      vis[x] = 1;
      t--;
      for (int i = hd[x]; i; i = nx[i]) {
        int y = v[i];
        if (vis[y]) continue;
        du[y]--;
        if (du[y] <= 1) {
          if (a[y] < 2)
            q[a[y]].push(y);
          else
            q[p].push(y);
        }
      }
    }
    p ^= 1;
    cnt++;
  }
  ans = min(ans, cnt);
}
void sol() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] == 0)
      a[i] = 2;
    else if (a[i] == 2)
      a[i] = 0;
  }
  init();
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    ade(u, v);
    ade(v, u);
  }
  ans = inf;
  o = 1;
  gg();
  o = 0;
  gg();
  printf("%d\n", ans);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    sol();
  }
  return 0;
}
