#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using vv = vector<vector<T>>;
template <class T>
inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T>
inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
template <class A, class B>
pair<A, B> operator+(const pair<A, B> &p, const pair<A, B> &q) {
  return {p.first + q.first, p.second + q.second};
}
template <class A, class B, class C, class D>
pair<A, B> &operator+=(pair<A, B> &p, const pair<C, D> &q) {
  p.first += q.first;
  p.second += q.second;
  return p;
}
template <class A, class B>
pair<A, B> operator-(const pair<A, B> &p, const pair<A, B> &q) {
  return {p.first - q.first, p.second - q.second};
}
template <class A, class B, class C, class D>
pair<A, B> &operator-=(pair<A, B> &p, const pair<C, D> &q) {
  p.first -= q.first;
  p.second -= q.second;
  return p;
}
template <class A, class B>
istream &operator>>(istream &is, pair<A, B> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T = ll>
T read() {
  T re;
  cin >> re;
  return move(re);
}
template <class T = ll>
T read(const T &dec) {
  T re;
  cin >> re;
  return re - dec;
}
template <class T = ll>
vector<T> readV(const int sz) {
  vector<T> re(sz);
  for (auto &x : re) x = read<T>();
  return move(re);
}
template <class T = ll>
vector<T> readV(const int sz, const T &dec) {
  vector<T> re(sz);
  for (auto &x : re) x = read<T>(dec);
  return move(re);
}
vv<int> readG(const int &n, const int &m) {
  vv<int> g(n);
  for (int(_) = 0; (_) < (int)(m); ++(_)) {
    const auto a = read<int>(1), b = read<int>(1);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  return move(g);
}
vv<int> readG(const int &n) { return readG(n, n - 1); }
const ll MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(0);
  auto T = read();
  while (T--) {
    const auto n = read();
    const auto cs = readV(n, 0);
    const auto g = readG(n);
    if (n == 1) {
      cout << 1 << "\n";
      continue;
    }
    int re = MOD;
    for (int(init) = (int)(1); (init) < (int)(3); ++(init)) {
      vector<queue<int>> que(3);
      vector<int> deg(n);
      for (int(v) = 0; (v) < (int)(n); ++(v)) deg[v] = g[v].size();
      for (int(v) = 0; (v) < (int)(n); ++(v))
        if (deg[v] == 1) que[cs[v]].emplace(v);
      int cur = init;
      int cnt = 0;
      int ex = n;
      while (ex) {
        ++cnt;
        ;
        while (que[0].size() + que[cur].size()) {
          int v;
          if (que[cur].size()) {
            v = que[cur].front();
            que[cur].pop();
          } else {
            v = que[0].front();
            que[0].pop();
          }
          --ex;
          ;
          for (const auto w : g[v])
            if (--deg[w] == 1) que[cs[w]].emplace(w);
        }
        cur = 3 - cur;
      }
      MN(re, cnt);
    }
    cout << re << "\n";
  }
  return 0;
}
