#include <bits/stdc++.h>
using namespace std;
void Read(int &x) {
  char c;
  while (!isdigit(c = getchar()))
    ;
  x = (c & 15);
  while (isdigit(c = getchar())) x = x * 10 + (c & 15);
}
void Print(int x) {
  int i;
  for (i = 1; i * 10LL <= x; i *= 10)
    ;
  for (; i; i /= 10) putchar(x / i % 10 | 48);
  putchar(10);
}
const int N(200005);
struct LnkNode {
  int v, nxt;
} edge[N << 1];
int etot, fst[N];
void addedge_(int u, int v) {
  ++etot;
  edge[etot].v = v;
  edge[etot].nxt = fst[u];
  fst[u] = etot;
}
void addedge(int u, int v) { addedge_(u, v), addedge_(v, u); }
int T, n, a[N], deg[N];
int q[3][N], Head[3], Tail[3];
int main() {
  Read(T);
  while (T--) {
    etot = 0, memset(fst + 1, 0, n << 2), memset(deg + 1, 0, n << 2),
    memset(Head, 0, 3 << 2), memset(Tail, 0, 3 << 2);
    Read(n);
    for (int i = 1; i <= n; ++i) Read(a[i]);
    for (int i = 1, u, v; i < n; ++i) {
      Read(u), Read(v);
      addedge(u, v);
      ++deg[u], ++deg[v];
    }
    for (int i = 1; i <= n; ++i)
      if (deg[i] <= 1) q[a[i]][Tail[a[i]]++] = i;
    int mn = 0x7fffffff, cnt = 0;
    while (Head[0] != Tail[0] || Head[1] != Tail[1] || Head[2] != Tail[2]) {
      ++cnt;
      while (Head[0] != Tail[0]) {
        int x = q[0][Head[0]++];
        for (int i = fst[x]; i; i = edge[i].nxt) {
          int &y = edge[i].v;
          if ((--deg[y]) == 1) q[a[y]][Tail[a[y]]++] = y;
        }
      }
      if (Head[1] != Tail[1]) {
        while (Head[1] != Tail[1]) {
          while (Head[1] != Tail[1]) {
            int x = q[1][Head[1]++];
            for (int i = fst[x]; i; i = edge[i].nxt) {
              int &y = edge[i].v;
              if ((--deg[y]) == 1) q[a[y]][Tail[a[y]]++] = y;
            }
          }
          while (Head[0] != Tail[0]) {
            int x = q[0][Head[0]++];
            for (int i = fst[x]; i; i = edge[i].nxt) {
              int &y = edge[i].v;
              if ((--deg[y]) == 1) q[a[y]][Tail[a[y]]++] = y;
            }
          }
        }
        continue;
      }
      if (Head[2] != Tail[2]) {
        while (Head[2] != Tail[2]) {
          while (Head[2] != Tail[2]) {
            int x = q[2][Head[2]++];
            for (int i = fst[x]; i; i = edge[i].nxt) {
              int &y = edge[i].v;
              if ((--deg[y]) == 1) q[a[y]][Tail[a[y]]++] = y;
            }
          }
          while (Head[0] != Tail[0]) {
            int x = q[0][Head[0]++];
            for (int i = fst[x]; i; i = edge[i].nxt) {
              int &y = edge[i].v;
              if ((--deg[y]) == 1) q[a[y]][Tail[a[y]]++] = y;
            }
          }
        }
        continue;
      }
    }
    mn = cnt;
    cnt = 0, memset(deg + 1, 0, n << 2), memset(Head, 0, 3 << 2),
    memset(Tail, 0, 3 << 2);
    for (int x = 1; x <= n; ++x)
      for (int i = fst[x]; i; i = edge[i].nxt) {
        int &y = edge[i].v;
        ++deg[y];
      }
    for (int i = 1; i <= n; ++i)
      if (deg[i] <= 1) q[a[i]][Tail[a[i]]++] = i;
    while (Head[0] != Tail[0] || Head[1] != Tail[1] || Head[2] != Tail[2]) {
      ++cnt;
      while (Head[0] != Tail[0]) {
        int x = q[0][Head[0]++];
        for (int i = fst[x]; i; i = edge[i].nxt) {
          int &y = edge[i].v;
          if ((--deg[y]) == 1) q[a[y]][Tail[a[y]]++] = y;
        }
      }
      if (Head[2] != Tail[2]) {
        while (Head[2] != Tail[2]) {
          while (Head[2] != Tail[2]) {
            int x = q[2][Head[2]++];
            for (int i = fst[x]; i; i = edge[i].nxt) {
              int &y = edge[i].v;
              if ((--deg[y]) == 1) q[a[y]][Tail[a[y]]++] = y;
            }
          }
          while (Head[0] != Tail[0]) {
            int x = q[0][Head[0]++];
            for (int i = fst[x]; i; i = edge[i].nxt) {
              int &y = edge[i].v;
              if ((--deg[y]) == 1) q[a[y]][Tail[a[y]]++] = y;
            }
          }
        }
        continue;
      }
      if (Head[1] != Tail[1]) {
        while (Head[1] != Tail[1]) {
          while (Head[1] != Tail[1]) {
            int x = q[1][Head[1]++];
            for (int i = fst[x]; i; i = edge[i].nxt) {
              int &y = edge[i].v;
              if ((--deg[y]) == 1) q[a[y]][Tail[a[y]]++] = y;
            }
          }
          while (Head[0] != Tail[0]) {
            int x = q[0][Head[0]++];
            for (int i = fst[x]; i; i = edge[i].nxt) {
              int &y = edge[i].v;
              if ((--deg[y]) == 1) q[a[y]][Tail[a[y]]++] = y;
            }
          }
        }
        continue;
      }
    }
    if (mn > cnt) mn = cnt;
    Print(mn);
  }
  return 0;
}
