#include<bits/stdc++.h>
#define ll        	long long 
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define pb          push_back
#define mp          make_pair
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define rrep(i,a,b) for(ll int i=a;i>=b;i--)
#define all(a)      (a).begin(),(a).end()
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#ifndef M_PI
    #define M_PI 3.14159265358979323846
#endif
#define tr(i, cont) for(auto i=cont.begin();i!=cont.end();i++)
#define ios        	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define trace(x)                 cerr << #x << ": " << x << "\n";
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << "\n";
#define endl "\n"
using namespace std;

ll val[200005];
ll deg[200005];
struct comp{
	bool operator()(ll a,ll b){
		if(val[a]<=val[b])
			return false;
		return true;
	}
};
int main(){
	ios;
	ll tc;
	cin>>tc;
	while(tc--){
		ll n;
		cin>>n;
		priority_queue<ll,vector<ll>,comp> pq;
		ll ans=0;
		rep(i,0,n){
			cin>>val[i];
			pq.push(i);
		}
		rep(i,0,n-1){
			ll u,v;
			cin>>u>>v;
			u--,v--;
			ans+=val[u]+val[v];
			deg[u]++;
			deg[v]++;
		}
		vi sol;
		sol.pb(ans);
		while(!pq.empty()){
			if(deg[pq.top()]>1){
				deg[pq.top()]--;
				ans-=val[pq.top()];
				sol.pb(ans);
				continue;
			}
			pq.pop();
		}
		rep(i,0,n){
			val[i]=0;
			deg[i]=0;
		}
		for(ll i=sz(sol)-1;i>=0;i--){
			cout<<sol[i]<<" ";
		}
		cout<<endl;
	}
}
