#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int mod = 1e9 + 7;
inline int sign(double x) { return (x > eps) - (x < -eps); }
inline int add(int a, int b) {
  a += b;
  return a < 0 ? a + mod : a >= mod ? a - mod : a;
}
inline int mul(long long a, long long b) { return a * b % mod; }
const int maxn = 5e5 + 10, INF = 0x3f3f3f3f;
pair<int, int> p[maxn], f[maxn][3];
vector<int> e[maxn];
int fa[maxn], col[maxn];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline void upd(pair<int, int>& a, int b) {
  if (b > a.first)
    a.second = a.first, a.first = b;
  else
    a.second = max(a.second, b);
}
int dia;
void dfs(int u, int fa) {
  for (int i = 1; i <= 2; ++i)
    f[u][i] = i == col[u] ? make_pair(1, 0) : make_pair(0, 0);
  for (auto v : e[u]) {
    if (v == fa) continue;
    dfs(v, u);
    if (!col[u]) {
      upd(f[u][1], f[v][1].first);
      upd(f[u][2], f[v][2].first);
    } else if (col[u] == 1) {
      upd(f[u][1], f[v][2].first + 1);
      upd(f[u][1], f[v][1].first);
    } else {
      upd(f[u][2], f[v][1].first + 1);
      upd(f[u][2], f[v][2].first);
    }
  }
  if (!col[u])
    dia = max(dia, max(f[u][1].first + f[u][2].first,
                       max(f[u][1].first + f[u][1].second - 1,
                           f[u][2].first + f[u][2].second - 1)));
  else
    dia = max(dia, f[u][col[u]].first + f[u][col[u]].second - 1);
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d", &col[i]), fa[i] = i, e[i].clear();
    for (int i = 1; i < n; ++i) {
      scanf("%d%d", &p[i].first, &p[i].second);
      int x = find(p[i].first), y = find(p[i].second);
      if (col[x] == col[y]) fa[x] = y;
    }
    for (int i = 1; i < n; ++i) {
      int x = find(p[i].first), y = find(p[i].second);
      if (col[x] != col[y]) e[x].push_back(y), e[y].push_back(x);
    }
    dia = 0;
    int rt = find(1);
    dfs(rt, 0);
    printf("%d\n", dia / 2 + 1);
  }
  return 0;
}
