#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, a[N], d[N];
vector<int> g[N];
void solve() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    g[i].clear();
    d[i] = 0;
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
  }
  int ans = n;
  {
    for (int i = 1; i <= n; i++) {
      d[i] = (int)(g[i]).size();
    }
    int cur = 0;
    int turn = 2;
    vector<int> que[3];
    for (int i = 1; i <= n; i++) {
      if (d[i] == 1) que[a[i]].push_back(i);
    }
    while ((int)(que[0]).size() + (int)(que[1]).size() + (int)(que[2]).size()) {
      while ((int)(que[0]).size() || (int)(que[turn]).size()) {
        if ((int)(que[0]).size()) {
          int u = que[0].back();
          que[0].pop_back();
          for (auto w : g[u]) {
            if (--d[w] == 1) que[a[w]].push_back(w);
          }
        }
        if ((int)(que[turn]).size()) {
          int u = que[turn].back();
          que[turn].pop_back();
          for (auto w : g[u]) {
            if (--d[w] == 1) que[a[w]].push_back(w);
          }
        }
      }
      cur++;
      turn = (turn == 2 ? 1 : 2);
    }
    ans = min(ans, cur);
  }
  {
    for (int i = 1; i <= n; i++) {
      d[i] = (int)(g[i]).size();
    }
    int cur = 0;
    int turn = 1;
    vector<int> que[3];
    for (int i = 1; i <= n; i++) {
      if (d[i] == 1) que[a[i]].push_back(i);
    }
    while ((int)(que[0]).size() + (int)(que[1]).size() + (int)(que[2]).size()) {
      while ((int)(que[0]).size() || (int)(que[turn]).size()) {
        if ((int)(que[0]).size()) {
          int u = que[0].back();
          que[0].pop_back();
          for (auto w : g[u]) {
            if (--d[w] == 1) que[a[w]].push_back(w);
          }
        }
        if ((int)(que[turn]).size()) {
          int u = que[turn].back();
          que[turn].pop_back();
          for (auto w : g[u]) {
            if (--d[w] == 1) que[a[w]].push_back(w);
          }
        }
      }
      cur++;
      turn = (turn == 2 ? 1 : 2);
    }
    ans = min(ans, cur);
  }
  ans = max(ans, 1);
  printf("%d\n", ans);
}
int32_t main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    solve();
  }
}
