#include <bits/stdc++.h>
#define ll long long 
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<ll, ll>
#define mod 1e9+7
#define inf 1e20
#define elif else if
#define fore(i,b) for(ll i=0;i<b;i++)
#define FORE(i,a,b) for(ll i=a;i<b;i++)
#define int long long

using namespace std;

ll n=0;


ll sol(int tc){
    cin>>n;
    vector<pair<int,int> > g(n);
    int tot=0;
    for(int i=0;i<n;i++){
        cin>>g[i].first;
        g[i].second=-1;
        tot+=g[i].first;
    }
    fore(i,n-1){
        int u,v;
        cin>>u>>v;
        g[u-1].second+=1;
        g[v-1].second+=1;
    }
    sort(g.begin(),g.end());
    int idx=n-1;
    while(idx>=0){
        if(g[idx].second>0) break;
        idx--;
    }
    cout<<tot<<" ";
    for(int i=2;i<n;i++){
        tot+=g[idx].first;
        g[idx].second--;
        while(idx>=0){
            if(g[idx].second>0) break;
            idx--;
        }
        cout<<tot<<" ";
    }
    cout<<endl;
    return 0LL;
} 

int32_t main()
{  

    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int tc=1;
    cin>>tc;
    while(tc--){
        sol(tc);
    }

    return 0;
} 

