#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T>
inline bool setmin(T &a, T b) {
  if (a > b) return a = b, 1;
  return 0;
}
template <class T>
inline bool setmax(T &a, T b) {
  if (a < b) return a = b, 1;
  return 0;
}
template <class T>
inline T fast(T a, T b, T mod) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
template <class T>
istream &operator>>(istream &os, vector<T> &container) {
  for (auto &u : container) os >> u;
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &container) {
  for (auto &u : container) os << u << " ";
  return os;
}
template <class T>
inline T gcd(T a, T b) {
  while (b) swap(a %= b, b);
  return abs(a);
}
const long long INF = 1e9 + 7;
const long long mod = 998244353;
const long long BIG_INF = 1e18 + 7;
const long long N = 1e6 + 7;
const long long T = 1 << 12;
const long double inf = 1e18;
int n;
int swap_col(int col) { return col ? 3 - col : 0; }
int solve(vector<vector<int> > &G, vector<int> &kolor) {
  vector<int> stopien(n);
  set<int> liscie[3];
  vector<int> erased(n);
  for (int i = 0; i < n; i++) {
    stopien[i] = (int)(G[i]).size();
    if ((int)(G[i]).size() <= 1) liscie[kolor[i] ? kolor[i] : 1].insert(i);
  }
  int col = 1;
  int ret = 0;
  while ((int)(liscie[col]).size() or (int)(liscie[swap_col(col)]).size()) {
    ret++;
    while ((int)(liscie[col]).size()) {
      auto akt = *liscie[col].begin();
      liscie[col].erase(liscie[col].begin());
      erased[akt] = 1;
      int ile_som = 0;
      for (auto &u : G[akt]) {
        if (!erased[u]) {
          ile_som++;
          stopien[u]--;
          if (stopien[u] == 1) liscie[(kolor[u] ? kolor[u] : col)].insert(u);
        }
      }
      assert(ile_som <= 1);
    }
    col = swap_col(col);
  }
  return ret;
}
void solve() {
  cin >> n;
  vector<vector<int> > G(n);
  vector<int> kolor(n);
  for (int i = 0; i < n; i++) cin >> kolor[i];
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int ans = INF;
  for (int i = 0; i < 2; i++) {
    ans = min(ans, solve(G, kolor));
    for (int j = 0; j < n; j++) kolor[j] = swap_col(kolor[j]);
  }
  cout << ans << '\n';
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int tests;
  cin >> tests;
  for (int i = 1; i <= tests; i++) solve();
}
