#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
vector<long long> vec[N];
long long t, n, w[N];
queue<long long> q[5];
bool vis[N];
long long du[N], deg[N];
long long solve(long long c) {
  for (long long i = 1; i <= n; i++) vis[i] = false;
  for (long long i = 1; i <= 2; i++)
    while (!q[i].empty()) q[i].pop();
  for (long long i = 1; i <= n; i++) du[i] = deg[i];
  for (long long i = 1; i <= n; i++) {
    if (du[i] == 1) {
      if (!w[i])
        q[c].push(i);
      else
        q[w[i]].push(i);
      vis[i] = 1;
    }
  }
  long long cnt = 0, res = 0, cur = c;
  while (cnt < n) {
    res++;
    while (!q[cur].empty()) {
      cnt++;
      long long u = q[cur].front();
      q[cur].pop();
      for (long long i = 0; i < vec[u].size(); i++) {
        long long v = vec[u][i];
        if (--du[v] == 1) {
          if (!w[v])
            q[cur].push(v);
          else
            q[w[v]].push(v);
        }
      }
    }
    cur = 3 - cur;
  }
  return res;
}
int main() {
  cin >> t;
  while (t--) {
    scanf("%lld", &n);
    for (long long i = 1; i <= n; i++) scanf("%lld", &w[i]);
    for (long long i = 1; i < n; i++) {
      long long u, v;
      scanf("%lld%lld", &u, &v);
      vec[u].push_back(v);
      vec[v].push_back(u);
      deg[u]++;
      deg[v]++;
    }
    if (n == 1) {
      puts("1");
      w[1] = 0;
      continue;
    }
    printf("%lld\n", min(solve(1), solve(2)));
    for (long long i = 1; i <= n; i++) w[i] = 0, deg[i] = 0;
    for (long long i = 1; i <= n; i++) vec[i].clear();
  }
  return 0;
}
