B. The Last Bit of Us
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ellie has a tree with $$$n$$$ nodes. Each node $$$i$$$ has a binary value $$$a_i$$$ which is either $$$0$$$ or $$$1$$$.

In a post-apocalyptic digital world, where every bit counts for survival, Ellie must eliminate all traces of digital presence by performing the following operation any number of times:

  • Select an edge $$$(u, v)$$$ in the tree. Then, simultaneously flip the values of $$$a_u$$$ and $$$a_v$$$ (i.e. change $$$0$$$ to $$$1$$$ and $$$1$$$ to $$$0$$$).

Ellie wants to convert all values in the tree to $$$0$$$ to escape detection. Your task is to determine if this is possible, and if so, find the minimum number of operations required.

Input

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.

The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 3 \cdot 10^5$$$) — the number of nodes in the tree.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i \in \{0, 1\}$$$) — the initial values of the nodes.

The next $$$n - 1$$$ lines describe the edges of the tree. Each line contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$), denoting an undirected edge between nodes $$$u$$$ and $$$v$$$. It is guaranteed that the given input forms a tree.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.

Output

For each test case, if it is impossible to convert all values to $$$0$$$, output $$$-1$$$. Otherwise, output the minimum number of operations required.

Example
Input
2
8
0 0 1 1 0 1 0 0
2 8
7 5
1 8
5 1
6 7
3 5
4 3
6
0 0 0 1 0 1
3 6
4 2
2 5
6 1
1 2
Output
-1
3
Note

In the first test case, it is impossible to convert all values to $$$0$$$, so the output is $$$-1$$$.

In the second test case, one possible sequence of operations with the minimum number of operations is:

  • Select the edge $$$(1, 2)$$$, so both $$$a_1$$$ and $$$a_2$$$ are flipped to $$$1$$$.
  • Select the edge $$$(4, 2)$$$, so both $$$a_4$$$ and $$$a_2$$$ are flipped to $$$0$$$.
  • Select the edge $$$(6, 1)$$$, so both $$$a_6$$$ and $$$a_1$$$ are flipped to $$$0$$$.
It can be shown that it is not possible to convert all values to $$$0$$$ in fewer than 3 operations.
Figure: tree from the second test case.