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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <bits/stdc++.h>

using namespace std;

#define JOIN_(X, Y) X##Y
#define JOIN(X, Y) JOIN_(X, Y)
#define TMP JOIN(tmp, __LINE__)
#define PB push_back
#define SZ(x) int((x).size())
#define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i)
#define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i)
#define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i)

#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif

typedef long long ll;
// typedef vector<int> vi;
// typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

// const int INF = 1e9 + 9;
// const ll INF =
const int MAX_N = 303;

ll my_abs(ll x) {
    if (x < 0) {
        return -x;
    }
    return x;
}

struct Best {
    ll power;
    ll removed_sum;
};

struct Tree;

struct Node {
    Tree *tree;
    int id;
    ll value;
    ll sum;
    int size;
    int level;
    vector<Best> best;
    vector<Node *> children;

    Node(Tree *_tree, int _id, ll _value, int level_)
        : tree(_tree), id(_id), value(_value), sum(_value), size(1), level(level_) {}

    ll tree_sum();

    void compute_best() {
        REP(i, size) { best.PB((Best){tree_sum() * tree_sum(), 0}); }
        for (auto &child : children) {
            child->compute_best();
        }
        int children_count = SZ(children);

        

        vector<int> current(children_count * 2), upper_limit(children_count * 2);
        REP(i, children_count) {
            upper_limit[i] = 1;
            upper_limit[children_count + i] = children[i]->size - 1;
        }
        DEB("id=" << id << " value=" << value << " children_count=" << children_count
                  << " size=" << size << "\n");
        DEB("upper_limit: ");
        REP(i, children_count * 2) { DEB(upper_limit[i] << ", "); }
        DEB("\n");
        for (;;) {
            int to_remove = 0;
            REP(i, children_count * 2) { to_remove += current[i]; }

            // DEB("  to_remove=" << to_remove << ": ");
            // REP(i, children_count * 2) { DEB(current[i] << ", "); }
            // DEB("\n");

            ll best_power = 0;
            ll left_sum = tree_sum();

            REP(i, children_count) {
                const auto &child = children[i];
                const Best &child_best = child->best[current[i + children_count]];
                if (current[i]) {
                    left_sum -= child->sum;
                } else {
                    left_sum -= child_best.removed_sum;
                }
            }
            // DEB("    left_sum=" << left_sum << "\n");

            REP(i, children_count) {
                const auto &child = children[i];
                const Best &child_best = child->best[current[i + children_count]];
                if (current[i]) {
                    ll up_sum = tree_sum() - child->sum;
                    ll removed_sum = child_best.removed_sum;
                    ll mid_sum = child->sum - removed_sum;
                    ll power_add = child_best.power - up_sum * up_sum - 2 * up_sum * mid_sum;

                    // DEB("      current[" << i << "]:\n");
                    // DEB("       tree_sum=" << tree_sum() << " child_sum=" << child->sum << "\n");
                    // DEB("       up_sum=" << up_sum << " mid_sum=" << mid_sum << " removed_sum="
                    //                      << removed_sum << " power_add=" << power_add << "\n");
                    best_power += power_add;
                } else {
                    ll up_mid_sum = tree_sum() - child_best.removed_sum;
                    ll power_add = child_best.power - up_mid_sum * up_mid_sum;

                    // DEB("      !current[" << i << "]:\n");
                    // DEB("       power_add=" << power_add << "\n");

                    best_power += power_add;
                }
            }

            best_power += left_sum * left_sum;
            // DEB("  power=" << best_power << " removed_sum=" << tree_sum() - left_sum
            //                << " left_sum=" << left_sum << "\n");
            if (best_power < best[to_remove].power or
                (best_power == best[to_remove].power and
                 (tree_sum() - left_sum) > best[to_remove].removed_sum)) {
                best[to_remove].power = best_power;
                best[to_remove].removed_sum = tree_sum() - left_sum;
            }
            // DEB("\n");

            if (to_remove == size - 1) {
                break;
            }
            ++current[children_count * 2 - 1];
            FORD(i, children_count * 2 - 1, 1) {
                if (current[i] <= upper_limit[i]) {
                    break;
                }
                current[i] = 0;
                ++current[i - 1];
            }
        }
        REP(i, size) {
            DEB(" i=" << i << " (" << best[i].power << " " << best[i].removed_sum << ")\n");
        }
        DEB("\n");
    }

    friend ostream &operator<<(ostream &o, const Node &node) {
        REP(i, node.level * 2) { o << " "; }
        o << "Node("
          << "v=" << node.value << ", "
          << "size=" << node.size << ", "
          << "sum=" << node.sum << ")\n";
        for (const auto &child : node.children) {
            o << *child;
        }
        return o;
    }

    void clear() {
        for (auto &child : children) {
            child->clear();
            delete child;
        }
    }

    pair<int, int> find_middle_size(int half_size) {
        pair<int, int> result(my_abs(half_size - size), id);
        for (auto &child : children) {
            auto child_result = child->find_middle_size(half_size);
            if (child_result < result) {
                result = child_result;
            }
        }
        return result;
    }

    pair<ll, int> find_middle_sum(ll half_sum) {
        pair<ll, int> result(my_abs(half_sum - sum), id);
        for (auto &child : children) {
            auto child_result = child->find_middle_sum(half_sum);
            if (child_result < result) {
                result = child_result;
            }
        }
        return result;
    }
};

struct Tree {
    Node *root;

    void build(int n, const vector<ll> &in, const vector<int> edges[], int start) {
        vector<bool> visited(n + 1);
        root = dfs(in, edges, visited, start);
    }

    Node *dfs(const vector<ll> &in, const vector<int> edges[], vector<bool> &visited, int x,
              int level = 0) {
        visited[x] = true;
        Node *result = new Node(this, x, in[x], level);
        for (int y : edges[x]) {
            if (not visited[y]) {
                Node *child = dfs(in, edges, visited, y, level + 1);
                result->children.PB(child);
                result->sum += child->sum;
                result->size += child->size;
            }
        }
        return result;
    }

    void compute_best() { root->compute_best(); }

    friend ostream &operator<<(ostream &o, const Tree &tree) {
        o << *tree.root;
        return o;
    }

    void clear() {
        root->clear();
        delete root;
    }
};

ll Node::tree_sum() { return tree->root->sum; }

template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {
    return os << "Pair(" << p.first << ", " << p.second << ")";
}

void inline one() {
    int n;
    cin >> n;
    vector<ll> in(n + 1);
    vector<int> edges[MAX_N];
    FOR(i, 1, n) {
        cin >> in[i];
        edges[i].clear();
    }
    REP(i, n - 1) {
        int a, b;
        cin >> a >> b;
        edges[a].PB(b);
        edges[b].PB(a);
    }
    Tree tree;
    tree.build(n, in, edges, 1);
    // DEB(tree << "\n");
    tree.compute_best();

    vector<ll> results(n);
    REP(i, n) { results[i] = tree.root->best[i].power; }

    int half_size = n / 2 + n % 2;
    auto middle_size_pair = tree.root->find_middle_size(half_size);
    int middle_size = middle_size_pair.second;
    DEB("middle_size: id=" << middle_size_pair << "\n");

    ll half_sum = tree.root->sum / 2 + tree.root->sum % 2;
    auto middle_sum_pair = tree.root->find_middle_sum(half_sum);
    int middle_sum = middle_sum_pair.second;
    DEB("middle_sum: id=" << middle_sum_pair << "\n");

    tree.clear();

    if (middle_size != 1) {
        tree = Tree();
        tree.build(n, in, edges, middle_size);
        tree.compute_best();
        REP(i, n) { results[i] = min(results[i], tree.root->best[i].power); }
        tree.clear();
    }

    if (middle_sum != 1 and middle_sum != middle_size) {
        tree = Tree();
        tree.build(n, in, edges, middle_sum);
        tree.compute_best();
        REP(i, n) { results[i] = min(results[i], tree.root->best[i].power); }
        tree.clear();
    }

    REP(i, n) { cout << results[i] << " "; }
    cout << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int z;
    cin >> z;
    while (z--)
        one();
}