#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 200010;
int tc, cs = 1;
int n, deg[N];
ll arr[N];
vector<int> adj[N];

void test_cases() {
  cin >> n;
  for (int i = 1; i <= n; ++i) deg[i] = 0;
  ll ans = 0;
  for (int i = 1; i <= n; ++i) cin >> arr[i], ans += arr[i];
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
    deg[u]++;
    deg[v]++;
  }
  priority_queue<pair<int, int>> pq;
  for (int i = 1; i <= n; ++i) {
    if (deg[i] > 1) pq.push({arr[i], i});
  }
  cout << ans;
  for (int i = 2; i < n; ++i) {
    if (pq.empty()) cout << " " << ans;
    else {
      auto u = pq.top();
      pq.pop();
      ans += u.first;
      deg[u.second]--;
      if (deg[u.second] > 1) pq.push({u.first, u.second});
      cout << " " << ans;
    }
  }
  cout << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> tc;
  while (tc--) {
    test_cases();
  }
}