#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
#include<string>
#include<map>
#include<numeric>
using namespace std;

signed main() {
	long long t;
	cin >> t;
	while (t--) {
		long long n;
		cin >> n;
		vector<long long> h(n), num(n);
		multimap<long long, long long> sl;
		for (auto& w : h) cin >> w;
		for (long long i = 0; i < n - 1; ++i) {
			long long u, v;
			cin >> u >> v; u--; v--;
			num[u]++;
			num[v]++;
		}
		for (long long i = 0; i < n; ++i) {
			if (num[i] > 1)
				sl.insert({ -h[i],num[i] });
		}
		long long sum = accumulate(h.begin(), h.end(), 0ll);
		cout << sum << ' ';
		for (long long i = 0; i < n - 2; ++i) {
			sum += -sl.begin()->first;
			cout << sum << ' ';
			sl.begin()->second--;
			if (sl.begin()->second == 1)
				sl.erase(sl.begin());
		}
		cout << '\n';
	}
}