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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "bits/stdc++.h"

using namespace std;
#define PB push_back
#define LL long long
#define int LL
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORD(i,a,b) for (int i = (a); i >= (b); i--)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define st first
#define nd second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define VI vector<int>
#define PII pair<int,int>
#define LD long double

template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream &os, vector<T> V){
  os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]";
} 

template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}


#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

struct Entry{
    int component; //weight
    int cost;
    bool operator<(const Entry & e) const{
        return tie(component, cost) < tie(e.component, e.cost);
    }
    bool operator>(const Entry & e) const{
        return tie(component, cost) > tie(e.component, e.cost);
    }
};

ostream & operator << (ostream & os, const Entry & e){
    return os << "(component="<<e.component<<",cost="<<e.cost <<")";
}

struct Dyn{
    vector<vector<Entry>> entries;
    int n;
    Dyn(int n_){
        n = n_;
        entries.resize(n);
    }
    void expandUp(int w){
        // add to current component
        vector<vector<Entry>> new_entries = entries;
        for(auto & v : new_entries){
            for(auto & e : v){
                e.cost += w * w + 2 * w * e.component;
                e.component += w;
            }
        }
        // start new component
        REP(i, n - 1){
            for(auto e: entries[i]){
                e.cost += w * w;
                e.component = w;
                new_entries[i + 1].push_back(e);
            }
        }
        entries = new_entries;
        makeConvex();
    }
    vector<pair<int,Entry>> getEntries() const{
        vector<pair<int, Entry>> res;
        REP(i, n){
            for(auto & e: entries[i]){
                res.push_back({i, e});
            }
        }
        return res;
    }
    void mergeRight(const Dyn & other){
        auto mine = getEntries();
        auto his = other.getEntries();
        vector<vector<Entry>> new_entries(n);
        for(auto p1: mine){
            for(auto p2: his){
                int idx = p1.st + p2.st;
                auto e1 = p1.nd;
                auto e2 = p2.nd;
                if(idx > n - 1){
                    continue;
                }
                //merge
                new_entries[idx].push_back({
                    e1.component + e2.component,
                    e1.cost + e2.cost +
                    2 * e1.component * e2.component
                });
                if(idx > n - 2){
                    continue;
                }
                //close
                new_entries[idx + 1].push_back({
                    e1.component,
                    e1.cost + e2.cost
                });
            }
        }
        entries = new_entries;
        makeConvex();
    }
    void makeConvex(){
        for(auto & v: entries){
            sort(ALL(v));
            if(v.empty()){
                continue;
            }
            int last_cost = v[0].cost;
            auto new_end = remove_if(
                v.begin() + 1,
                v.end(),
                [&](const Entry & e){
                    bool res = e.cost >= last_cost;
                    last_cost = e.cost;
                    return res;
                }
            );
            v.erase(new_end, v.end());
        }
    }
    VI getRes(){
        VI res;
        for(auto & v: entries){
            int best = LLONG_MAX;
            for(auto & e: v){
                mini(best, e.cost);
            }
            res.push_back(best);
        }
        return res;
    }
};

struct Solution{
    VI weights;
    vector<VI> graph;
    VI depths;
    int n;
    Solution(){
        cin >> n;
        depths.resize(n);
        weights.resize(n);
        graph.resize(n);
        REP(i, n){
            cin >> weights[i];
        }
        REP(i, n - 1){
            int a,b;
            cin >> a >> b;
            a--;
            b--;
            graph[a].PB(b);
            graph[b].PB(a);
        }
    }
    VI solve(){
        dfs2(0);
        auto it = max_element(ALL(depths));
        Dyn r = dfs(it - depths.begin());
        return r.getRes();
    };
    void dfs2(int w, int d = 0, int p = -1){
        depths[w] = d;
        int ans = 1;
        for (int a: graph[w]){
            if(a == p)
                continue;
            dfs2(a, d+1, w);
        }
    }
    Dyn dfs(int w, int p = -1){
        vector<Dyn> r;
        for(int a : graph[w]){
            if(a == p){
                continue;
            }
            r.emplace_back(dfs(a, w));
        }
        if(r.empty()){
            Dyn res(n);
            res.entries[0].push_back({weights[w], weights[w] * weights[w]});
            debug(w + 1, res.entries);
            return res;
        }
        Dyn cur = r[0];
        cur.expandUp(weights[w]);
        for(int i = 1; i < SZ(r); i++){
            cur.mergeRight(r[i]);
        }
        debug(w+1, cur.entries);
        return cur;
    }
};

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  int t;
  cin>> t;
  REP(_, t){
    VI v = Solution().solve();
    REP(i, SZ(v)){
        cout << v[i] << " ";
    }
    cout << "\n";
  }
}