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
300
301
302
303
304
305
306
307
308
309
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; }

struct Line {
	long long partial, squares;
	int cuts;
	long long a() const {
		return 2 * partial;
	}
	long long b() const {
		return squares + partial * partial;
	}
	Line operator +(const Line& he) const {
		return Line{partial + he.partial, squares + he.squares, cuts + he.cuts};
	}
	void self_cut() {
		cuts++;
		squares += partial * partial;
		partial = 0;
	}
	long double inter(const Line& he) const {
		// ax+b == a2x+b2
		assert(a() > he.a());
		return (long double) (he.b() - b()) / (a() - he.a());
	}
};

const int MAX_K = INT_MAX;

int cmp(long long a, long long b) {
	if(a > b) {
		return 1;
	}
	if(a == b) {
		return 0;
	}
	return -1;
}

map<int,int> freq;

void trim(vector<Line>& lines) {
	int memo_size = lines.size();
	// return;
	sort(lines.begin(), lines.end(), [&](const Line& one, const Line& two) {
		return one.a() > two.a() || (one.a() == two.a() && one.b() < two.b());
	});
	int max_k = 0;
	for(const Line& line : lines) {
		max_k = max(max_k, line.cuts);
	}
	vector<vector<Line>> hulls(max_k + 1);
	for(Line C : lines) if(C.cuts <= MAX_K) {
		++freq[C.cuts];
		vector<Line>& ch = hulls[C.cuts];
		if(!ch.empty() && ch.back().a() == C.a()) {
			continue;
		}
		while((int) ch.size() >= 2) {
			Line A = ch.end()[-2];
			Line B = ch.end()[-1];
			if(A.inter(B) < B.inter(C)) {
				break;
			}
			ch.pop_back();
		}
		// if(ch.empty() || ch.back().inter(C) < 8123123) {
		ch.push_back(C);
		// }
	}
	lines.clear();
	for(const vector<Line>& ch : hulls) {
		lines.insert(lines.end(), ch.begin(), ch.end());
	}
	// if(memo_size >= 50'000) {
		// cerr << memo_size << " -> " << lines.size() << endl;
	// }
}

struct Test {
	vector<int> value;
	vector<vector<int>> edges;
	vector<int> subtree;
	int MAX_VALUE;
	
	
	vector<Line> dfs(int a, int parent) {
		subtree[a] = 1; //value[a];
		// int SUM = accumulate(value.begin(), value.end(), 0);
		int n = value.size();
		// SUM *= (n + 5 * sqrt(n)) / n;
		
		vector<Line> mine;
		mine.push_back(Line{value[a], 0, 0});
		for(int b : edges[a]) {
			if(b == parent) {
				continue;
			}
			vector<Line> they = dfs(b, a);
			subtree[a] += subtree[b];
			
			vector<Line> combined;
			vector<pair<long long, long long>> hack[301];
			for(const Line& me : mine) {
				for(const Line& he : they) {
					int k = me.cuts + he.cuts;
					if((long long) (me.partial + he.partial) * max(1, k - 2) <= MAX_VALUE * subtree[a]) {
						hack[k].emplace_back(me.partial+he.partial, me.squares+he.squares);
					}
					if((long long) me.partial * max(1, k+1 - 2) <= MAX_VALUE * subtree[a]) {
						hack[k+1].emplace_back(me.partial, me.squares+he.squares+he.partial*he.partial);
					}
				}
			}
			int all = 0, only = 0;
			for(int k = 0; k <= subtree[a]; ++k) {
				vector<pair<long long, long long>> nowe;
				if(hack[k].size() <= 500) {
					nowe = hack[k];
				}
				else {
					vector<long long> small(1205, LONG_LONG_MAX);
					for(const pair<long long, long long>& p : hack[k]) {
						int i = p.first>>18;
						small[i] = min(small[i], p.second);
					}
					for(int i = 1; i < (int) small.size(); ++i) {
						small[i] = min(small[i-1], small[i]);
					}
					for(const pair<long long, long long>& p : hack[k]) {
						++all;
						int i = p.first>>18;
						if(i == 0 || p.second < small[i-1]) {
							++only;
							nowe.push_back(p);
							// combined.push_back(Line{p.first, p.second, k});
						}
					}
				}
				sort(nowe.begin(), nowe.end());
				long long pref_small = LONG_LONG_MAX;
				for(const pair<long long, long long>& p : nowe) {
					++all;
					if(p.second >= pref_small) {
						continue;
					}
					++only;
					combined.push_back(Line{p.first, p.second, k});
					pref_small = min(pref_small, p.second);
				}
			}
			
			// if(all >= 1'000'000) {
				// cerr << "HACK: " << all << " -> " << only << " " << imie(a) imie(b) imie(parent);
				// for(int x : edges[a]) cerr << "  ," << x;
				// cerr << endl;
			// }
					
			if(0)for(Line me : mine) {
				for(Line he : they) {
					int k = me.cuts + he.cuts;
					// subtree[a]
					if(k <= MAX_K && me.partial + he.partial <= 2 * (k <= 50 ? 800'000 : 1'000'000) * subtree[a] / max(1, k - 8)) { //me.partial + he.partial <= 3 * SUM / max(1, me.cuts + he.cuts - 10)) { //        (me.cuts + he.cuts <= 8 || me.partial + he.partial <= 25'000'000)) {
						combined.push_back(me + he);          //  me.partial + he.partial <= n * 500'000 / k * 2
						// cerr << "p=" << he.partial << " ";
						// if(he.partial >= 800'000 || a == 0 || (int) edges[a].size() >= 3) {
							he.self_cut();
							combined.push_back(me + he);
						// }
					}
				}
			}
			mine = combined;
			trim(mine);
		}
		// trim(mine);
		return mine;
	}
	
	/*vector<long long> dfs(int a, int parent) {
		vector<long long> dp{0, 1};
		for(int b : edges[a]) {
			if(b == parent) {
				continue;
			}
			vector<long long> his = dfs(b, a);
			vector<long long> nowe(int(dp.size() + his.size()) - 1);
			for(int i = 0; i < (int) dp.size(); ++i) {
				for(int j = 0; j < (int) his.size(); ++j) {
					nowe[i+j] += dp[i] * his[j];
				}
			}
			dp = nowe;
		}
		dp[0]++;
		return dp;
	}*/

	void test_case() {
		int n;
		scanf("%d", &n);
		subtree.resize(n);
		value.resize(n);
		edges.resize(n);
		int multi = 1;
		MAX_VALUE = 0;
		for(int i = 0; i < n; ++i) {
			scanf("%d", &value[i]);
			MAX_VALUE = max(MAX_VALUE, value[i]);
		}
		multi = 1'000'000 / MAX_VALUE;
		MAX_VALUE *= multi;
		MAX_VALUE *= 1.1;
		for(int i = 0; i < n; ++i) {
			value[i] *= multi;
		}
		for(int i = 0; i < n - 1; ++i) {
			int x, y;
			scanf("%d%d", &x, &y);
			x--;
			y--;
			edges[x].push_back(y);
			edges[y].push_back(x);
		}
		// debug() << imie(dfs(0, -1));
		// debug();
		
		/*
		vector<int> deg;
		int at_least_three = 0;
		for(int i = 0; i < n; ++i) {
			deg.push_back(edges[i].size());
			if((int) edges[i].size() >= 3) {
				++at_least_three;
			}
			// cerr << edges[i].size() << " ";
		}
		sort(deg.rbegin(), deg.rend());
		debug() << imie(at_least_three);
		debug() << imie(deg);
		debug();
		// cerr << endl;
		return;*/
		int root = 0;
		// for(int a = 0; a < n; ++a) {
			// if(edges[a].size() < edges[root].size()) {
				// root = a;
			// }
		// }
		vector<Line> all = dfs(root, -1);
		vector<long long> answer(n + 1, LONG_LONG_MAX);
		for(Line me : all) {
			me.self_cut();
			answer[me.cuts] = min(answer[me.cuts], me.squares);
		}
		cerr << "size = " << all.size() << endl;
		for(int i = 1; i <= n; ++i) {
			if(answer[i] == LONG_LONG_MAX) {
				answer[i] = -1;
			}
			printf("%lld ", answer[i] / multi / multi);
			// printf("%d %lld\n", i, answer[i] / multi / multi);
		}
		puts("");
		// for(pair<int,int> p : freq) {
			// cerr << p.first << ": " << p.second << endl;
		// }
		// cerr << endl;
		freq.clear();
	}
};

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		Test t;
		t.test_case();
	}
}