#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7, inf = 0x3f3f3f3f, mod = 1e9 + 7;
vector<int> g[maxn];
int n, col[maxn];
int solve(int x) {
  vector<int> d(n + 1);
  vector<vector<int> > leaf(3);
  for (int i = 1; i <= n; ++i) {
    d[i] = g[i].size();
    if (d[i] == 1) leaf[col[i]].push_back(i);
  }
  int ans = 0;
  while (1) {
    ans++;
    int id;
    while (leaf[x].size() || leaf[0].size()) {
      if (leaf[x].size())
        id = x;
      else
        id = 0;
      int u = leaf[id].back();
      leaf[id].pop_back();
      for (auto &v : g[u])
        if (--d[v] == 1) leaf[col[v]].push_back(v);
    }
    x ^= 3;
    if (leaf[x].size() == 0 && leaf[0].size() == 0) return ans;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i <= n; ++i) g[i].clear();
    for (int i = 1; i <= n; ++i) cin >> col[i];
    for (int i = 0, u, v; i < n - 1; ++i) {
      cin >> u >> v;
      g[u].push_back(v);
      g[v].push_back(u);
    }
    int a = solve(1);
    int b = solve(2);
    cout << min(a, b) << '\n';
  }
  return 0;
}
