#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 3e5+10, oo = 1e9+10;
void solve() {
	int n;
	cin >> n;
	vector<ll> a(n+1);
	ll ans = 0;
	vector<ll> answers;
	answers.reserve(n-1);
	for(int i = 1; i <= n; ++i) {
		cin >> a[i];
		ans += a[i];
	}
	answers.push_back(ans);
	vector<vector<ll>> edges(n+1);
	vector<ll> deg(n+1);
	for(int i = 0; i < n - 1; ++i) {
		ll u, v;
		cin >> u >> v;
		++deg[u];
		++deg[v];
	}
	vector<ll> can_take;
	for(int i = 1; i <= n; ++i) {
		for(int x = 0; x < deg[i] - 1; ++x) {
			can_take.push_back(a[i]);
		}
	}
	sort(can_take.begin(), can_take.end());
	while(answers.size() < n - 1) {
		answers.push_back(can_take.back() + answers.back());
		can_take.pop_back();
	}
	assert(answers.size() == n - 1);
	for(ll x : answers) {
		cout << x << ' ';
	}
	cout << '\n';
}
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int T = 1;
	cin >> T;
	while(T--) {
		solve();
	}
}
