#include <bits/stdc++.h>
const int maxn = 1e6 + 222;
int n, d[maxn], dgr[maxn], a[maxn];
std::vector<int> ch[maxn];
int work(int c) {
  for (int i = 1; i <= n; ++i) d[i] = dgr[i];
  std::vector<int> q[3], s;
  int cnt = n;
  for (int i = 1; i <= n; ++i)
    if (d[i] == 1) {
      q[a[i]].push_back(i);
    }
  int ans = 0;
  while (cnt) {
    ans++;
    while (q[0].size() || q[c].size()) {
      for (int u : q[0]) {
        cnt--;
        for (int v : ch[u]) {
          d[v]--;
          if (d[v] == 1) s.push_back(v);
        }
      }
      for (int u : q[c]) {
        cnt--;
        for (int v : ch[u]) {
          d[v]--;
          if (d[v] == 1) s.push_back(v);
        }
      }
      q[0].clear();
      q[c].clear();
      for (int u : s) {
        q[a[u]].push_back(u);
      }
      s.clear();
    }
    c = 3 - c;
  }
  return ans;
}
void solve() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) {
    ch[i].clear();
    dgr[i] = 0;
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    ch[u].push_back(v);
    ch[v].push_back(u);
    dgr[u]++;
    dgr[v]++;
  }
  if (n == 1) {
    puts("1");
    return;
  }
  printf("%d\n", std::min(work(1), work(2)));
  return;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
  return 0;
}
