Capital Movement

Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef is playing a video game. In a video game, there's a advanced civilization that has a total of N planets under control. All of those planets are connected with N1 teleports in such a way, that it's possible to travel between any two planets using those teleports.
There's a chance that some planet gets infected. In this case it takes 24 hours for civilization to find out infection and prevent it from spreading. During this time infection uses teleport one time and infect all the planets that can be achieved in one teleport jump. So, once infection is detected at planet V, scientists already know that all planets connected to V via teleport are also infected. All the neccessary teleports are disabled right away and medics start working on eliminating the infection.
Each planet has population. Planets are numbered from 1 to N and their populations are P_{1}, P_{2}, ..., P_{N}. It is known that all the P_{i} are distinct.
There's a capital among all those planets. The capital is known to have the biggest population.
Once infection is detected at planet V, after disabling teleports on planet V and all connected to them, government has to establish a new capital if needed in the remaining notinfected planets. So, they list all the planets that are not connected to V and are not V. Then they pick the planet with biggest population. Your task is to find the number of this planet for every possible V.
Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains one integer N.
Next line contains N spaceseparated integers P_{1}, P_{2}, ..., P_{N} denoting the population of each planet.
Next N1 lines contain two spaceseparated integers each V and U denoting that there's a teleport between planet V and U.
Output
For each test case, output a single line containing N integers A_{1}, A_{2}, ..., A_{N} separated by a space. Here A_{i} denotes the number of the planet picked to be new capital in case infection starts spreading from the planet i. In case infection affects all the planets output 0.
Constraints
 1 ≤ T ≤ 5
 1 ≤ N ≤ 50000
 1 ≤ P_{i} ≤ 10^{9}
 All P_{i} are distinct
 1 ≤ V ≤ N
 1 ≤ U ≤ N
Subtasks
 Subtask #1 (20 points): N ≤ 100
 Subtask #2 (30 points): N ≤ 10000
 Subtask #3 (50 points): No additional constraints
Example
Input: 1 6 5 10 15 20 25 30 1 3 2 3 3 4 4 5 4 6 Output: 6 6 6 2 6 5
Author:  cenadar 
Tester:  iscsi 
Date Added:  8082015 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.3.2, CPP 4.9.2, CPP14, CS2, D, ERL, FORT, FS, GO, HASK, ICK, ICON, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYPY, PYTH, PYTH 3.4, RUBY, SCALA, SCM chicken, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions