#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long int
#define ull unsigned long long
#define ld long double
 
//#define mod 1000000007
#define EULER 2.7182818284
#define INF 1000000000
 
#define pb push_back
#define ins insert
#define IT iterator
#define PQ priority_queue
#define min_heap priority_queue<ll,vector<ll>,greater<ll>>
#define max_heap priority_queue<ll>
 
#define fi first
#define se second
#define maxe max_element
#define mine min_element
#define er erase
#define lb lower_bound
#define ub upper_bound
 
#define vi vector<int>
#define vll vector<ll>
#define vb vector<bool>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpll vector<pair<ll,ll>>
//#define unordered_map ump 
 
#define mem(v,i) memset(v,i,sizeof(v)) // v is array. To set all elements to 0 or -1 only. mem(arr,0);
 
 
#define fo(i,a,b) for(int i=(a);i<=(b);i++)
#define fod(i,a,b) for(int i=(a);i>=(b);i--)
#define FORl(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORN(i,n) for(int i=(0);i<(n);i++)
#define FORR(i,n) for(int i=(n);i>=0;i--)
#define bs bitset
#define bpc(a) __builtin_popcount(a)
 
ll max(ll a,ll b)
{
    if(a>b)return a;return b;
}
ll min(ll a,ll b)
{
    if(a<b)return a;return b;
}
ll msb(ll n)
{    
    ll ans;
    for(ll i=31; i>=0; i--)
     if((1LL<<i)&(n))
       {ans=i;
       break;}
     return ans;  
}

// size of array and mod(in general)

const int N = 300500;
const int mod = 1000000007;
ll fact[N];
ll invFact[N];

// -----------------power(a,b)%mod -------------
ll fast_pow(ll a, ll p) {
    ll res = 1;
    while (p) {
        if (p % 2 == 0) {
            a = (a * a) % mod;
            p /= 2;
        } else {
            res = (res * a) % mod;
            p--;
        }
    }
    return res;
}


 //------------------- nCr % N------------------------------- 

// copy in main function

/*
  fact[0] = invFact[0] = 1;
    for (int i = 1; i < N; i++) {
        fact[i] = (fact[i - 1] * i) % mod;
        invFact[i] = fast_pow(fact[i], mod - 2);
    }
*/

// Now fact[i] stores factorial(n) % mod;
// and invFact[i] stores modulo_inverse(b) i.e, power(b,mod-2)

ll C(int n, int k) {
    if (k > n) {
        return 0;
    }
    return fact[n] * invFact[k] % mod * invFact[n - k] % mod;
}

//-------------------------------------------------
vector<ll>primes;
void sieve(ll n)// n mtlb jaha tak chaiye 
{ 
    bool prime[n+1]; 
    memset(prime, true, sizeof(prime)); 
  
    for (ll p=2; p<=n; p++) 
    { 
        // If prime[p] is not changed, then it is a prime 
        if (prime[p] == true) 
        { 
            for (ll i=p*p; i<=n; i += p) 
                prime[i] = false; 
        } 
    } 
  
    for (ll p=2; p<=n; p++) 
       if (prime[p]) 
            primes.pb(p); 
} 

vector<pair<int,int>> find_exponents(int n) // like p = 2^p1 * 3^p2....
{
	vector<pair<int,int>>exp; // prime no. and it's frequency
	for(int i=2; i<=n; i++)
	{
		if(n%i==0)
		{
			int cnt=0;
			while(n%i==0)
			{
				cnt++;
				n/=i;
			}
			exp.push_back({i,cnt});
		}
	}
	
	if(n!=1)
	exp.push_back({n,1});
    return exp;

}

// ---------------------------------segment trees(1 index hi lena) ----------------------------------------

ll ar[100001],st[400001]; 
void build(int si, int ss, int se)
{
     if(se==ss) //leaf node (ss se hi deal krna ar[ss] ka, because si to segment tree ka hai)
     {
     	st[si]=ar[ss];
     	return;
     }
     //vrna recursively left aur right child pehle solve kro fir merge kro
     
     int mid =(ss+se)/2;
     build(2*si,ss,mid);
     build(2*si+1,mid+1,se);
     
     // ab jo st[si] hai vo left aur right child ko merge krne se milega
     st[si] = min(st[2*si], st[2*si+1]); 
}

// min element range query
int query(int si, int ss, int se, int qs, int qe) // vo segment lene hai jo [qs,qe] mai completely lie kre. else recursion
{
	if(ss>qe || se<qs)
		return INT_MAX;
	
	if(ss>=qs && se<=qe)
	    return st[si];
	
	int mid = (ss+se)/2;
	
	return min( query(2*si,ss,mid,qs,qe) , query(2*si+1,mid+1,se,qs,qe) );
	
	
}
// qi index vale element ko agar change krna h(ar mai change kr dena pehle hi)
void update(int si, int ss, int se, int qi)
{
	if(ss==se)// leaf and qi also
	{
		st[si]=ar[ss];
		return;
	}

	int mid = (ss+se)/2;
	if(qi<=mid)
	update(2*si,ss,mid,qi);
	else
	update(2*si+1,mid+1,se,qi);

	st[si]=min(st[2*si],st[2*si+1]);
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

vector<ll>adj[100001], rev[100001];
bool vis[100001],recur[100001];
ll val[100001], in[100001];
void init(ll n)
{
    for(ll i=0; i<=n; i++)
    {
        adj[i].clear();
        rev[i].clear();
        vis[i]=false;
        recur[i]=false;
        in[i]=0;
    }
}




//-----------------------------------------------------------paindi aa fer dhakk champion!!--------------------------------------------------------------------------------


int main() {
	ios_base::sync_with_stdio(0);
	 cin.tie(0); cout.tie(0);
	ll t;
    cin>>t;
     while(t--)
	 {
	     ll n;
	     cin>>n;
	     ll sum = 0;
	     init(n);
	     priority_queue<pair<ll,ll>> pq;
	     for(int i=1; i<=n; i++)
	       {  cin>>val[i];
	         sum += val[i];
	       }
	     
	     for(int i=0; i<n-1; i++)
	     {
	         int x,y;
	         cin>>x>>y;
	         in[x]++;
	         in[y]++;
	     }
	     
	     for(int i=1; i<=n; i++)
	     {
	         if(in[i]>1) // kyoki ==1 vala leaf hai, usko ek color assign kr hi dia bacha hua
	           pq.push({val[i],in[i]-1});           // poochne vala kaam max weight leaf se krna hai. usse bhi ek baar to ek color de hi dia hai.
	                    // to isliye in[i]-1 daalenge priority queue mai. 
	     }
	     
	     cout<<sum<<" ";
	     
	     for(int i=2; i<=n-1; i++)
	     {
	         auto p = pq.top();
	         pq.pop();
	         
	         sum += p.first;
	         if(p.second>1) //vrna ye edge include ho gyi. ab baaki h ismei to theek vrna doosri dekho
	          {p.second--;
	          pq.push(p);}
	          cout<<sum<<" ";
	     }
	     cout<<endl;
	     
	     
	 }
	 
    


	return 0;
}