#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    vector<int> a(n);
    vector<vector<int>> V(n);
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 1; i < n; ++i) {
      int u, v;
      cin >> u >> v;
      --v, --u;
      V[u].push_back(v);
      V[v].push_back(u);
    }
    int l = -1, r = n + 1;
    while (r - l > 1) {
      int m = (l + r) >> 1;
      vector<vector<int>> d(n, vector<int>(2, n + 1));
      function<void(int, int)> dfs = [&](int u, int f) {
        int M[2][2] = {};
        for (int v : V[u])
          if (v != f) {
            dfs(v, u);
            for (int i = 0; i < 2; ++i) {
              int vl = min(d[v][i], d[v][i ^ 1] + 1);
              for (int j = 0; j < 2; ++j)
                if (M[i][j] < vl) swap(vl, M[i][j]);
            }
          }
        if (a[u] != 2 && M[0][1] + M[0][0] <= m) d[u][0] = M[0][0];
        if (a[u] != 1 && M[1][1] + M[1][0] <= m) d[u][1] = M[1][0];
      };
      dfs(0, 0);
      if (d[0][0] <= m || d[0][1] <= m)
        r = m;
      else
        l = m;
    }
    cout << (r + 1) / 2 + 1 << "\n";
  }
  return 0;
}
