#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
#define int long long

signed main() {
    int t;
    cin >> t;
    for (int gob = 0; gob < t; ++gob) {
        int n;
        cin >> n;
        vector<int> w(n);
        vector<int> c(n);
        for (int i = 0; i < n; ++i) {
            cin >> w[i];
        }
        int ab, ba;
        for (int i = 0; i < n - 1; ++i) {
            cin >> ab >> ba;
            c[ab - 1]++;
            c[ba - 1]++;
        }
        multiset<int> asdgg;
        int joke = 0;
        for (int i = 0; i < n; ++i) {
            joke += w[i] * c[i];
            for (int j = 0; j < c[i] - 1; ++j) {
                asdgg.insert(w[i]);
            }
        }
        vector<int> aksdhf;
        for (int i = 0; i < n - 1; ++i) {
            aksdhf.push_back(joke);
            if (asdgg.size() > 0) {
                joke -= *asdgg.begin();
                asdgg.erase((asdgg.begin()));
            }
        }
        reverse(aksdhf.begin(), aksdhf.end());
        for (auto x : aksdhf) {
            cout << x << " ";
        }
        cout << "\n";
    }
}