#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const int MAXINT = 1073741823;
int color_of[MAXN];
vector<long long int> neighbors[MAXN];
long long int distance_of[MAXN];
void dfs(long long int v, long long int parent, long long int color) {
  if (parent != -1) distance_of[v] = distance_of[parent];
  if (parent != -1 && color_of[v] != color && color_of[v] != 0) {
    distance_of[v]++;
    color = color_of[v];
  }
  for (int i : neighbors[v])
    if (i != parent) dfs(i, v, color);
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long int t, n, counter, a, b, maximum, root, answer;
  cin >> t;
  for (int q = 0; q < t; q++) {
    cin >> n;
    for (long long int i = 0; i < n; i++) neighbors[i].clear();
    counter = 0;
    for (long long int i = 0; i < n; i++) {
      cin >> color_of[i];
      if (color_of[i] == 0) counter++;
    }
    for (long long int i = 0; i < n - 1; i++) {
      cin >> a >> b;
      a--;
      b--;
      neighbors[a].push_back(b);
      neighbors[b].push_back(a);
    }
    if (counter == n) {
      cout << 1 << '\n';
      continue;
    }
    for (long long int i = 0; i < n; i++)
      if (color_of[i]) {
        dfs(i, -1, color_of[i]);
        break;
      }
    maximum = -MAXINT;
    for (long long int i = 0; i < n; i++)
      if (color_of[i] && distance_of[i] > maximum) {
        maximum = distance_of[i];
        root = i;
      }
    for (long long int i = 0; i < n; i++) distance_of[i] = 0;
    dfs(root, -1, color_of[root]);
    answer = -1;
    for (long long int i = 0; i < n; i++) answer = max(answer, distance_of[i]);
    cout << (answer + 1) / 2 + 1 << '\n';
  }
  return 0;
}
