#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll, ll>
#define inf 0x3f3f3f3f 
#define MAX 200005
#define pb push_back
#define fastcin  ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

void solve(){
    ll n, x, y, sum=0;
    cin >> n;
    ll w[n], degree[n];
    memset(degree, 0, sizeof degree);
    vector<ll>ans, next;
    for(int i=0; i<n; i++) cin >> w[i];
    for(int i=0; i<n-1; i++){
        cin >> x >> y;
        x--; y--;
        degree[x]++;
        degree[y]++;
    }
    for(int i=0; i<n; i++) sum+=w[i];
    ans.pb(sum);
    for(int i=0; i<n; i++){
        for(int j = 0; j < degree[i]-1; j++){
            next.pb(w[i]);
        }
    }
    sort(next.rbegin(), next.rend());
    for(auto it : next){
        sum += it;
        ans.pb(sum);
    }
    for(auto it : ans){
        cout << it << ' ';
    }
    cout << endl;
}
int main(){
    fastcin
    int t;
    cin >> t;
    while(t--)solve();
    return 0;
}
