#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 110;
vector<long long> a[N];
vector<pair<long long, long long> > vec[N][2];
long long c[N];
long long dp[N][2];
long long ans = 0;
void dfs(long long v, long long p = -1) {
  dp[v][0] = 0;
  dp[v][1] = 0;
  if (c[v] != -1) dp[v][c[v]] = 1;
  for (auto u : a[v]) {
    if (u == p) continue;
    dfs(u, v);
    long long tmp[2];
    if (c[v] != -1) {
      dp[v][c[v]] = max(dp[v][c[v]], dp[u][c[v]]);
      dp[v][c[v]] = max(dp[v][c[v]], dp[u][1 - c[v]] + 1);
    } else {
      for (long long i = 0; i < 2; i++) {
        dp[v][i] = max(dp[v][i], dp[u][i]);
      }
    }
    vec[v][0].push_back({dp[u][0], u});
    vec[v][1].push_back({dp[u][1], u});
  }
  if (vec[v][0].size() <= 1) {
    return;
  }
  for (long long i = 0; i < 2; i++) {
    sort(vec[v][i].begin(), vec[v][i].end(),
         greater<pair<long long, long long> >());
  }
  for (long long i = 0; i < 2; i++) {
    long long f = vec[v][i][0].first, s = vec[v][i][1].first;
    if (c[v] == 1 - i) {
      ans = max(ans, f + s + 1);
    } else {
      ans = max(ans, f + s - 1);
    }
  }
  long long ff = vec[v][0][0].first, fs = vec[v][1][0].first;
  long long sf = vec[v][0][1].first, ss = vec[v][1][1].first;
  if (vec[v][0][0].second != vec[v][1][0].second) {
    ans = max(ans, ff + fs);
  } else {
    ans = max(ans, max(ff + ss, sf + fs));
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  cin >> t;
  for (long long ii = 0; ii < t; ii++) {
    long long n;
    cin >> n;
    ans = 0;
    long long root = -1;
    for (long long i = 0; i < n; i++) {
      a[i].clear();
      vec[i][0].clear();
      vec[i][1].clear();
      cin >> c[i];
      if (c[i] == 1) {
        root = i;
      }
      c[i]--;
    }
    for (long long i = 0, u, v; i < n - 1; i++) {
      cin >> u >> v;
      u--;
      v--;
      a[u].push_back(v);
      a[v].push_back(u);
    }
    if (root == -1) {
      cout << "1\n";
      continue;
    }
    dfs(root);
    for (long long i = 0; i < n; i++) {
      ans = max(ans, max(dp[i][0], dp[i][1]));
    }
    cout << (ans + 2) / 2 << "\n";
  }
  return 0;
}
