#include <bits/stdc++.h>
using namespace std;
mt19937_64 mrand(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 200005;
int a[maxn];
vector<int> g[maxn];
queue<int> q[3];
int calc(int cur, int n, vector<int> deg) {
  int cnt = 0, ret = 0;
  vector<int> vis(n + 1);
  for (int i = 1; i <= 2; ++i)
    while (!q[i].empty()) q[i].pop();
  for (int i = 1; i <= n; ++i)
    if (deg[i] == 1) {
      if (!a[i])
        q[cur].push(i);
      else
        q[a[i]].push(i);
      vis[i] = 1;
    }
  while (cnt != n) {
    ++ret;
    while (!q[cur].empty()) {
      int u = q[cur].front();
      q[cur].pop();
      cnt++;
      for (int v : g[u]) {
        if (vis[v]) continue;
        if (--deg[v] == 1) {
          vis[v] = 1;
          if (!a[v])
            q[cur].push(v);
          else
            q[a[v]].push(v);
        }
      }
    }
    cur = 3 - cur;
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    if (n == 1) {
      cout << 1 << endl;
      continue;
    }
    for (int i = 1; i <= n; ++i) g[i].clear();
    vector<int> deg(n + 1);
    for (int i = 1; i < n; ++i) {
      int u, v;
      cin >> u >> v;
      g[u].push_back(v);
      g[v].push_back(u);
      deg[u]++;
      deg[v]++;
    }
    cout << min(calc(1, n, deg), calc(2, n, deg)) << endl;
  }
}
