1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>

#define FOR(i, b, e) for (int i = (b); i <= (e); i++)
#define FORD(i, b, e) for (int i = (e); i >= (b); i--)
#define MP make_pair
#define FS first
#define ND second
#define PB push_back
#define ALL(x) x.begin(), x.end()

using namespace std;

using LL = long long;
using PII = pair<int,int>;
using PLL = pair<LL,LL>;
using VI = vector<int>;
using VLL = vector<LL>;
template<class T>
using V = vector<T>;

template<class T>
int sz(const T& a) { return (int)a.size(); }
template<class T>
void amin(T& a, T b) { a = min(a, b); }
template<class T>
void amax(T& a, T b) { a = max(a, b); }

const int inf = 1e9;
const LL infl = 1e18;

int t, n, a[31], s[31];
V<PII> g[31];
int m;

LL ans[31];
stack<int> st;

void dfs(int u, int p)
{
  s[st.top()] += a[u];
  for (auto [i, v] : g[u]) if (v != p) {
    if (m & (1 << (i - 1))) st.push(v);
    dfs(v, u);
    if (m & (1 << (i - 1))) st.pop();
  }
}

int main()
{
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    FOR(i, 1, n) scanf("%d", &a[i]);

    FOR(i, 1, n - 1) {
      int u, v;
      scanf("%d %d", &u, &v);
      g[u].PB(MP(i, v));
      g[v].PB(MP(i, u));
    }

    FOR(i, 1, n) ans[i] = infl;

    int M = (1 << (n - 1)) - 1;
    FOR(b, 0, M) {
      m = b;
      FOR(i, 1, n) s[i] = 0;
      st.push(1);
      dfs(1, 1);
      st.pop();

      int k = __builtin_popcount(b);
      LL x = 0;
      FOR(i, 1, n) x += s[i] * s[i];
      amin(ans[k + 1], x);
    }

    FOR(i, 1, n) {
      printf("%lld ", ans[i]);
      g[i].clear();
    }
    putchar('\n');
  }
}