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
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <array>
#include <random>
#include <cmath>
#include <list>
#include <ctime>
#include <sstream>
#include <queue>
#include <climits>
#include <stack>
#include <random>
#include <bitset>
#include <numeric>
#include <cassert>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
#define rep(x, b, e) for(int x=(b); x<(e); ++x)
#define trav(a, x) for(auto& a : x)
#define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x)
#define all(c) c.begin(),c.end()
#define sz(x) ((int)((x).size()))
#define pb push_back
#define st first
#define nd second
#define mp(x,y) make_pair(x,y)
typedef short int sint;

#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int N = 301;
const ll inf = 1000000000000000000LL; // 10^18
int n;
vi weights;
vi za[N];

struct Zbior {
	set<pair<ll, ll>> zbior;

	void insert(ll total, ll root) {
		auto me = zbior.find({total, root});
		if (me != zbior.end()) {
			// had already same result
			return ;
		}
		auto it = zbior.lower_bound({total, root});
		if (it != zbior.begin()) {
			--it;
			if (it->nd <= root) {
				return ;
			}
		}
		it = zbior.lower_bound( { total, root } );
		while (it != zbior.end() && root <= it->nd) {
			auto elToRemove = *it;
			++it;
			zbior.erase(elToRemove);
		}
		zbior.insert({ total, root });
	}
};

struct Node {
	Zbior f[N];

	void print(int vert) {
		return;
		printf("For vert: %d\n", vert);
		int w1 = 1;
		int total = 0;
		while (sz(f[w1].zbior)) {
			printf("\t k: %d, sz: %d\n", w1, sz(f[w1].zbior));
			total += sz(f[w1].zbior);
			trav(el, f[w1].zbior) {
				printf("\t\t(closed sum: %lld, root size: %lld)\n", el.st, el.nd);
			}
			++w1;
		}
		printf("\t total: %d\n", total);
	}
};


Node f[N];

void clear() {
	rep(i, 0, n) {
		za[i].clear();
		rep(j, 0, n + 1) {
			f[i].f[j].zbior.clear();
		}
	}
}

Node merge(const Node& parent, const Node& child) {
	Node res;
	int w1 = 1;
	while (sz(parent.f[w1].zbior)) {
		trav(apos, parent.f[w1].zbior) {
			int w2 = 1;
			// odciecie
			while (sz(child.f[w2].zbior)) {
				trav(bpos, child.f[w2].zbior) {
					// apos + bpos
					res.f[w1 + w2].insert( apos.st + bpos.st, apos.nd );
				}
				++w2;
			}
			w2 = 1;
			// polaczenie
			ll lcost = apos.st - (apos.nd * apos.nd);
			while (sz(child.f[w2].zbior)) {
				trav(bpos, child.f[w2].zbior) {
					ll rcost = bpos.st - (bpos.nd * bpos.nd);
					ll new_root = apos.nd + bpos.nd;
					res.f[w1 + w2 - 1].insert(lcost + rcost + new_root * new_root, new_root );
				}
				++w2;
			}
		}
		++w1;
	}
	return res;
}

void compute(int v, int par) {
	// printf("visiting: %d par: %d\n", v, par);
	f[v].f[1].insert( (ll)weights[v]*weights[v], weights[v] );
	trav(u, za[v]) {
		if (u == par) {
			continue;
		}
		compute(u, v);
		// printf("will merge: %d and %d\n", v, u);
		f[v] = merge(f[v], f[u]);
	}
	f[v].print(v);
}

void testc() {
	scanf("%d", &n);
	weights.resize(n);
	rep(i, 0, n) scanf("%d", &weights[i]);
	rep(i, 0, n - 1) {
		int x, y;
		scanf("%d %d", &x, &y);
		--x; --y;
		za[x].pb(y);
		za[y].pb(x);
	}

	compute(0, -1);
	ll r = inf;
	rep(w1, 1, n + 1) {
		trav(el, f[0].f[w1].zbior) {
			r = min(r, el.st);
		}
		printf("%lld ", r);
	}
	printf("\n");
	clear();
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	int t;
	scanf("%d", &t);
	while (t--) {
		testc();
	}
}