//#define LOCAL
#include <bits/stdc++.h>
#define fore(i, a, b) for (ll i = (a), _b = (b); i < (_b); i++)
#define fort(i, a, b) for (ll i = (a), _b = (b); i <= (_b); i++)
#define ford(i, a, b) for (ll i = (a), _b = (b); i >= (_b); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define lowbit(x) ((x) & (-(x)))
#define children(x) num[x], num[x] + d[x] - 1
using namespace std;

template<class A, class B> bool mini(A &a, const B &b) {return a > b ? (a = b, 1) : 0; }
template<class A, class B> bool maxi(A &a, const B &b) {return a < b ? (a = b, 1) : 0; }

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;

const ll maxN = 1e5 + 5;

ll t, n, p, ans, w[maxN], idx[maxN], num[maxN], d[maxN];
vi adj[maxN];

struct FenwickTree {
    ll bit[maxN];
    ll get(ll x) {
        ll res = 0;
        for (; x > 0; x -= lowbit(x))
            res += bit[x];
        return res;
    }
    void upd(ll x) {
        for (; x <= n; x += lowbit(x))
            bit[x]++;
    }
    ll get(ll l, ll r) {
        return get(r) - get(l - 1);
    }
    void build() {
        fort(i, 1, n)
            bit[i] = 0;
    }
} bit;

void dfs(ll u, ll par) {
    num[u] = ++p;
    d[u]++;
    for (ll &v : adj[u]) {
        if (v == par)
            continue;
        dfs(v, u);
        d[u] += d[v];
    }
}

bool cmp(const ll &l,
         const ll &r) {
    return w[l] > w[r];
}

int main() {
    #ifdef LOCAL
        freopen("d.INP", "r", stdin);
//        freopen("d.OUT", "w", stdout);
    #endif // LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    while (t--) {
        ll u, v, deg;
        //prepare
        cin >> n;
        ans = 0;
        fort(i, 1, n) {
            cin >> w[i];
            ans += w[i];
            adj[i].clear();
            idx[i] = i;
            d[i] = 0;
        }
        fore(_, 1, n) {
            cin >> u >> v;
            adj[u].pb(v);
            adj[v].pb(u);
        }
        sort(idx + 1, idx + 1 + n, cmp);
        bit.build();
        p = 0;
        dfs(1, 0);
        u = 1;
        v = idx[u];
        deg = sz(adj[v]) - 1;
        //solve
        cout << ans << ' ';
        fort(_, 1, n - 2) {
            while (!deg) {
                u++;
                v = idx[u];
                deg = sz(adj[v]);
                if (bit.get(children(v)) < u - 1)
                    deg--;
                for (ll &i : adj[v]) {
                    if (bit.get(children(i)))
                        deg--;
                }
            }
            ans += w[v];
            deg--;
            cout << ans << ' ';
        }
        cout << '\n';
    }
    return 0;
}
