#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 1e9;
const int N = 2e5 + 10;
int n, c[N];
vector<int> adj[N];
int dp[N][2], diam;
void dfs(int u, int p) {
  dp[u][0] = dp[u][1] = INF;
  vector<vector<int> > mx(2, vector<int>(2));
  for (auto v : adj[u])
    if (v != p) {
      dfs(v, u);
      for (int i = 0; i < (int)(2); i++) {
        auto cand = min(dp[v][0] + (i != 0), dp[v][1] + (i != 1));
        if (cand > mx[i][0]) {
          mx[i][1] = mx[i][0];
          mx[i][0] = cand;
        } else if (cand > mx[i][1]) {
          mx[i][1] = cand;
        }
      }
    }
  for (int i = 0; i < (int)(2); i++) {
    if (c[u] != -1 && c[u] != i) continue;
    if (mx[i][0] + mx[i][1] <= diam) dp[u][i] = mx[i][0];
  }
}
bool good(int x) {
  diam = x;
  dfs(0, 0);
  return dp[0][0] < INF || dp[0][1] < INF;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    cin >> n;
    for (int i = 0; i < (int)(n); i++) cin >> c[i], c[i]--;
    for (int _ = 0; _ < (int)(n - 1); _++) {
      int u, v;
      cin >> u >> v;
      u--;
      v--;
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    int lo = -1, hi = n + 2;
    while (lo + 1 < hi) {
      auto mi = (lo + hi) / 2;
      if (good(mi))
        hi = mi;
      else
        lo = mi;
    }
    cout << (hi + 1) / 2 + 1 << '\n';
    for (int u = 0; u < (int)(n); u++) adj[u].clear();
  }
  return 0;
}
