#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void ckmax(T& x, T y) {
  x = (y > x ? y : x);
}
template <typename T>
inline void ckmin(T& x, T y) {
  x = (y < x ? y : x);
}
namespace Fread {
const int SIZE = 1 << 21;
char buf[SIZE], *S, *T;
inline char getchar() {
  if (S == T) {
    T = (S = buf) + fread(buf, 1, SIZE, stdin);
    if (S == T) return EOF;
  }
  return *S++;
}
}  // namespace Fread
namespace Fwrite {
const int SIZE = 1 << 21;
char buf[SIZE], *S = buf, *T = buf + SIZE;
inline void flush() {
  fwrite(buf, 1, S - buf, stdout);
  S = buf;
}
inline void putchar(char c) {
  *S++ = c;
  if (S == T) flush();
}
struct NTR {
  ~NTR() { flush(); }
} ztr;
}  // namespace Fwrite
namespace Fastio {
struct Reader {
  template <typename T>
  Reader& operator>>(T& x) {
    char c = Fread ::getchar();
    T f = 1;
    while (c < '0' || c > '9') {
      if (c == '-') f = -1;
      c = Fread ::getchar();
    }
    x = 0;
    while (c >= '0' && c <= '9') {
      x = x * 10 + (c - '0');
      c = Fread ::getchar();
    }
    x *= f;
    return *this;
  }
  Reader& operator>>(char& c) {
    c = Fread ::getchar();
    while (c == '\n' || c == ' ') c = Fread ::getchar();
    return *this;
  }
  Reader& operator>>(char* str) {
    int len = 0;
    char c = Fread ::getchar();
    while (c == '\n' || c == ' ') c = Fread ::getchar();
    while (c != '\n' && c != ' ') {
      str[len++] = c;
      c = Fread ::getchar();
    }
    str[len] = '\0';
    return *this;
  }
  Reader() {}
} cin;
const char endl = '\n';
struct Writer {
  template <typename T>
  Writer& operator<<(T x) {
    if (x == 0) {
      Fwrite ::putchar('0');
      return *this;
    }
    if (x < 0) {
      Fwrite ::putchar('-');
      x = -x;
    }
    static int sta[45];
    int top = 0;
    while (x) {
      sta[++top] = x % 10;
      x /= 10;
    }
    while (top) {
      Fwrite ::putchar(sta[top] + '0');
      --top;
    }
    return *this;
  }
  Writer& operator<<(char c) {
    Fwrite ::putchar(c);
    return *this;
  }
  Writer& operator<<(char* str) {
    int cur = 0;
    while (str[cur]) Fwrite ::putchar(str[cur++]);
    return *this;
  }
  Writer& operator<<(const char* str) {
    int cur = 0;
    while (str[cur]) Fwrite ::putchar(str[cur++]);
    return *this;
  }
  Writer() {}
} cout;
}  // namespace Fastio
const int MAXN = 2e5;
int n, a[MAXN + 5];
pair<int, int> e[MAXN + 5];
int fa[MAXN + 5], sz[MAXN + 5];
int get_fa(int u) { return (fa[u] == u) ? u : (fa[u] = get_fa(fa[u])); }
void unite(int u, int v) {
  u = get_fa(u);
  v = get_fa(v);
  if (u != v) {
    if (sz[u] > sz[v]) swap(u, v);
    fa[u] = v;
    sz[v] += sz[u];
  }
}
struct EDGE {
  int nxt, to;
} edge[MAXN * 2 + 5];
int head[MAXN + 5], tot;
inline void add_edge(int u, int v) {
  edge[++tot].nxt = head[u];
  edge[tot].to = v;
  head[u] = tot;
}
bool vis[MAXN + 5];
int _deg[MAXN + 5], deg[MAXN + 5];
int leaves[MAXN + 5], cnt_leaf;
int work(int c) {
  cnt_leaf = 0;
  for (int i = 1; i <= n; ++i) {
    vis[i] = 0;
    deg[i] = _deg[i];
    if (deg[i] <= 1) {
      leaves[++cnt_leaf] = i;
      vis[i] = 1;
    }
  }
  int res = 0;
  while (cnt_leaf) {
    res++;
    queue<int> q;
    for (int i = 1; i <= cnt_leaf; ++i) {
      int u = leaves[i];
      if (a[u] == 0 || a[u] == c) {
        q.push(u);
        if (i != cnt_leaf) {
          swap(leaves[i], leaves[cnt_leaf]);
        }
        --cnt_leaf;
        --i;
      }
    }
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = head[u]; i; i = edge[i].nxt) {
        int v = edge[i].to;
        if (vis[v]) continue;
        deg[v]--;
        if (deg[v] <= 1) {
          vis[v] = 1;
          if ((a[v] == 0 || a[v] == c)) {
            q.push(v);
          } else {
            leaves[++cnt_leaf] = v;
          }
        }
      }
    }
    c = 3 - c;
  }
  return res;
}
void solve_case() {
  Fastio ::cin >> n;
  for (int i = 1; i <= n; ++i) {
    Fastio ::cin >> a[i];
    fa[i] = i;
    sz[i] = 1;
    head[i] = 0;
    _deg[i] = 0;
  }
  tot = 0;
  for (int i = 1; i < n; ++i) {
    int u, v;
    Fastio ::cin >> u >> v;
    if (a[u] == a[v]) unite(u, v);
    e[i] = make_pair(u, v);
  }
  for (int i = 1; i < n; ++i) {
    int u = e[i].first, v = e[i].second;
    u = get_fa(u);
    v = get_fa(v);
    if (u != v) {
      add_edge(u, v);
      add_edge(v, u);
      _deg[u]++;
      _deg[v]++;
    }
  }
  int ans = n;
  for (int i = 1; i <= 2; ++i) {
    ckmin(ans, work(i));
  }
  Fastio ::cout << ans << Fastio ::endl;
}
int main() {
  int T;
  Fastio ::cin >> T;
  while (T--) {
    solve_case();
  }
  return 0;
}
