#include<bits/stdc++.h>
using namespace std;
#define ll long long
int a[100100]={0},deg[200020]={0};
int tong[200020]={0};
int main(){
    ll n,i,j,t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        ll sum=0;
        for(i=1;i<=n;i++)cin>>a[i],deg[i]=0,sum+=a[i];
        vector<int>v;
        for(i=0;i<n-1;i++){
            int x,y;
            cin>>x>>y;
            deg[x]++;
            deg[y]++;
            if(deg[x]>1)v.push_back(a[x]);
            if(deg[y]>1)v.push_back(a[y]);
        }
        cout<<sum<<" ";
        if(v.size()==0){cout<<endl;continue;}
        sort(v.begin(),v.end());

        for(i=v.size()-1;i>=0;i--){
            sum+=v[i];
            cout<<sum<<" ";
        }
        cout<<endl;
    }
}
