#include <bits/stdc++.h>
using namespace std;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vc<vc<T>>;
template <class T>
void mkuni(vector<T> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}
long long rand_int(long long l, long long r) {
  static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
  return uniform_int_distribution<long long>(l, r)(gen);
}
template <class T>
void print(T x, int suc = 1) {
  cout << x;
  if (suc == 1)
    cout << '\n';
  else
    cout << ' ';
}
template <class T>
void print(const vector<T> &v, int suc = 1) {
  for (int i = 0; i < v.size(); i++)
    print(v[i], i == (int)(v.size()) - 1 ? suc : 2);
}
const int maxn = 2e5 + 7;
const int mod = 998244353;
int col[maxn], d[maxn], fa[maxn], c[maxn];
vector<int> g[maxn];
int n;
int solve(int x) {
  vector<int> leaf[3];
  vector<int> d(n + 1);
  for (int i = 1; i <= n; ++i) {
    d[i] = g[i].size();
    if (d[i] == 1) leaf[col[i]].emplace_back(i);
  }
  int ans = 0, id;
  while (true) {
    ++ans;
    while (leaf[x].size() || leaf[0].size()) {
      if (leaf[x].size())
        id = x;
      else
        id = 0;
      int cur = leaf[id].back();
      leaf[id].pop_back();
      for (auto &u : g[cur]) {
        if (--d[u] == 1) leaf[col[u]].emplace_back(u);
      }
    }
    x ^= 3;
    if (leaf[x].size() == 0 && leaf[0].size() == 0) return ans;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> col[i];
    for (int i = 1; i < n; ++i) {
      int u, v;
      cin >> u >> v;
      g[u].emplace_back(v);
      g[v].emplace_back(u);
      ++d[u], ++d[v];
    }
    int a, b;
    a = solve(1);
    b = solve(2);
    print(min(a, b));
    for (int i = 1; i <= n; ++i) g[i].clear();
  }
  return 0;
}
