//submitted by sldinkar
#include <algorithm>
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mod (int)1000000007
#define MOD (int)10000000007
#define infinity (int)(1e18)

#define all(a) a.begin(),a.end()
#define ll long long
#define int long long
#define pb push_back
#define precision(n) fixed<<setprecision(n)
#define endl "\n"
#define no "NO"
#define yes "YES"


#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define bloop(i, a, b) for (int i = a ; i>=b;i--)
#define test(t) int t; cin >> t; while (t--)
#define init(a, i) memset(a, i, sizeof(a))


#define vi vector<int>
#define vll vector<ll>
#define pi pair<int, int>
#define vii vector<pi>

#define f first

int gcd(int a, int b) {
    if (a == 0) return b;
    return gcd(b % a, a);
}

int lcm(int a, int b) { return a * b / gcd(a, b); }

int max(int a, int b) { if (a > b) return a; else return b; }

int min(int a, int b) { if (a < b) return a; else return b; }

bool isPrime(int N) {
    for (int i = 2; i * i <= N; ++i) { if (N % i == 0) return false; }
    return true;
}

bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); }



// main code starts from here

signed main() {
    fast;
    test(t) {
        int n;
        cin >> n;
        int arr[n];
        vector<int> v[n + 1], v1;
        int x, y;
        for (int i = 0; i < n; i++) cin >> arr[i];
        for (int i = 0; i < n - 1; i++) {
            cin >> x >> y;
            v[x].push_back(y);
            v[y].push_back(x);
        }
        for (int i = 1; i <= n; i++) {
            while (v[i].size() != 1) {
                v1.push_back(arr[i - 1]);
                v[i].pop_back();
            }
        }
        sort(v1.begin(), v1.end(), greater<int>());
        int total = 0;
        for (int i = 0; i < n; i++) total += arr[i];
        cout << total << " ";
        for (int i = 0; i < v1.size(); i++) {
            total += v1[i];
            cout << total << " ";
        }
        cout << endl;
    }
    return 0;
}