Colourful Christmas Bauble Burglary
Indra loves a good gamble. So much so, that they gambled away all of their money. Desperate, they have concocted a plan to make some back.
Installed at the centre of town is a Christmas tree which has valuable baubles
of various colours. The tree has baubles, with bauble
at the root of the tree, and
branches which connects one bauble to
another. It is known that all baubles are connected to each other via some sequence of branches.
The more baubles of a single colour they have, the more valuable they are. Specifically,
if Indra has baubles of any one colour, the total value of all of those baubles is
.
Indra plans on sneakily cutting a branch and stealing part of the tree to sell the baubles. Specifically, if Indra removes some branch, then they can steal and sell all baubles which are no longer connected to the root. However, they must carefully choose which branch to cut, as stealing more of the tree will likely incur more risk.
To help Indra evaluate their options, for each branch, calculate the value they can gain after cutting that branch.
Hint: A recursive solution may run into stack overflow.
Input
Each test contains multiple test cases. The first line contains the number of test cases (
). The description of the test cases follows.
The first line of each test case contains an integer
(
), the number of baubles in the Christmas tree.
The second line of each test case contains
integers
(
), where
denotes the
colour of bauble
.
The following
lines contains two integers
, denoting that a branch exists from bauble
to bauble
.
It is guaranteed that the sum of over all testcases is less than or equal to
.
Output
For each testcase, output a line containing integers
, where
is the value Indra gains
from cutting the
-th branch.
Examples
Input 1
3
7
3 2 3 3 3 2 2
1 3
3 2
2 4
7 2
6 4
5 4
6
3 1 6 5 3 2
6 5
6 1
6 2
1 4
3 1
5
3 2 3 3 1
2 3
5 3
1 3
2 4
Output 1
18 13 5 1 1 1
1 3 1 1 1
2 1 6 1
Comments