#include<bits/stdc++.h>
using namespace std;
// typedef long long ll;
#define ll long long int
#define p(x) cout<<#x<<" = "<<x<<"\n";
#define FIN ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ALL(a) a.begin(),a.end()
#define mset(a,b) memset(a,b,sizeof(a));
 
// int area(int a);
int main()
{
    int t;
    cin>>t;
    // FIN;
    while(t--)
    {
        int n;
        cin>>n;
        ll arr[n+1];
        ll sum=0;
        int deg[n+1]; 
        priority_queue<pair<ll,ll>>pq;
        for(int i=1;i<=n;i++)
        {
            cin>>arr[i];
            sum+=arr[i];
            deg[i]=0;
            
        }
       
              
        for(int i=1;i<=n-1;i++)
        {
            ll a,b;
            cin>>a>>b;
            deg[a]++,deg[b]++;
        }
        for(int i=1;i<=n;i++)
        {
            if(deg[i]>1)
            {
                // p(i);
                pq.push({arr[i],i});
            }
            
        }
        for(int i=1;i<=n-1;i++)
        {
            if(i==1)
            cout<<sum<<" ";
            else
            {
                ll a=pq.top().first;
                ll b=pq.top().second;

                pq.pop();
                if(deg[b]>1)
                sum+=a;

                deg[b]--;
                if(deg[b]>1)
                {
                    pq.push({a,b});
                }
                cout<<sum<<" ";
            }
        }
        cout<<endl;
    }
}