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
#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#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 PPC(x) __builtin_popcount(x)
#define MSB(x) (63 - __builtin_clzll(x))
#define SZ(x) ((int)(x).size())
#define HASK(S, x) (S.find(x) != S.end())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i) & 1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std; 

template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b);	}

const int maxN = 304;
const long long INF = 1000000000000000000ll;

vector <int> G[maxN];
int T[maxN], vis[maxN], visCtr, n;
pair <int, int> E[maxN];
vector <int> inds;

bool taken[maxN];
vector <int> des;
tuple <long long, int, int> prop;
long long sz[maxN][maxN];

void clear()
{
	FOR(i, 1, n+1)
		G[i].resize(0), vis[i] = 0;
	visCtr = 0;
}

int oth(int e, int v)
{	return E[e].ft ^ E[e].sd ^ v;	}

long long dfs(int v, int fet=0)
{
	long long& ret = sz[v][fet];
	ret = T[v];
	vis[v] = visCtr;
	for (int e : G[v]) if (!taken[e] and e != fet)
		ret += dfs(oth(e, v), e);
	return ret;
}

void sfd(int v, int fet, long long comp)
{
	for (int e : G[v]) if (e != fet)
	{
		int u = oth(e, v);
		sz[v][e] = comp - (taken[e] ? 0 : sz[u][e]);
		if (!taken[e])
			sfd(u, e, comp);
	}
}

long long licz(int r)
{
	long long comp = dfs(r);
	sfd(r, 0, comp);
	return comp;
}

void procVer(int v, int fet, long long p, long long q)
{
	for (int e : G[v]) if (!taken[e])
	{
		int u = oth(e, v);
		long long s = sz[u][e];
		remin(prop, make_tuple(2ll * (p-s) * (q-s), fet, e));
	}
}

void procEdge(int e)
{
	auto [a, b] = E[e];
	long long p = sz[a][e], q = sz[b][e];
	procVer(a, e, p, q);
	procVer(b, e, q, p);	
}

bool step(long long& val)
{
	prop = make_tuple(INF, 0, 0);
	for (int e : des)
		procEdge(e);
		
	auto [c, old, nw] = prop;
		
	if (c >= 0)
		return false;
	
	for (int& e : des) if (e == old)
	{
		e = nw;
		break;
	}
	swap(taken[old], taken[nw]);
	val += c;
	licz(E[nw].ft), licz(E[nw].sd);
	return true;
}

long long gen(int k)
{
	fill(taken, taken+n, false);
	des.resize(0);
	random_shuffle(ALL(inds));
	FOR(i, 0, k)
	{
		int e = inds[i];
		taken[e] = true;
		des.pb(e);
	}
	
	visCtr++;
	long long ret = 0;
	FOR(i, 1, n+1) if (vis[i] != visCtr)
	{
		long long x = licz(i);
		ret += kw(x);
	}
	return ret;
}

void solve()
{
	scanf ("%d", &n);
	FOR(i, 1, n+1)
		scanf ("%d", T+i);
	FOR(i, 1, n)
	{
		int a, b;
		scanf ("%d%d", &a, &b);
		G[a].pb(i);
		G[b].pb(i);
		E[i] = {a, b};
	}
	inds.resize(n-1);
	iota(ALL(inds), 1);
	
	FOR(k, 0, n)
	{
		long long res = INF;
		FOR(_, 0, 5)
		{
			long long val = gen(k);
			while (step(val)) ;
			remin(res, val);			
		}
		printf("%lld ", res);
	}
	printf("\n");
	clear();	
}
 
int main()
{
	int t = 1;
	scanf ("%d", &t);
	FOR(tid, 1, t+1)
	{
		//printf("Case #%d: ", tid);
		solve();
	}
	return 0;
}