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
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
//mt19937 mrand(random_device{}()); 
const ll mod=1000000007;
//int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=310;

struct point {
	ll x,y;
	point() {}
	point(ll x,ll y):x(x),y(y) {} 
}p[N];
bool operator < (const point &a, const point &b) {
	return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
bool operator == (const point &a, const point &b) {
	return a.x==b.x&&a.y==b.y;
}
int h,t,q[N];
bool cmp(point a,point b,point c) {
	// b在c上面
	b.x-=a.x, b.y-=a.y;
	c.x-=a.x, c.y-=a.y;
	if (b.x==c.x) return 0;
	long double fa=(long double)(b.y)*c.x,fb=(long double)(b.x)*c.y;
	if (abs(fa-fb)>=1e15) {
		return fa>=fb;
	} else {
		return b.y*c.x-b.x*c.y>=0ll;
	}
}

/*

bool cmp2(point a,point b,point c) {
	// b在c上面
	b.x-=a.x, b.y-=a.y;
	c.x-=a.x, c.y-=a.y;
	if (b.x==c.x) return 0;
	return (__int128_t)b.y*c.x-(__int128_t)b.x*c.y>=0;
}*/


void rebuild(vector<point> a,vector<point> &b) {
	b.clear();
	sort(all(a));
	a.erase(unique(all(a)),a.end());
	h=0; t=-1;
	rep(i,0,SZ(a)) {
		while (t>=1&&cmp(a[q[t-1]],a[q[t]],a[i])) {
			//assert(cmp2(a[q[t-1]],a[q[t]],a[i])==cmp(a[q[t-1]],a[q[t]],a[i]));
			--t;
		}
		q[++t]=i;
	}
	while (t>=1&&a[q[t]].y>=a[q[t-1]].y) --t;
	rep(i,0,t+1) b.pb(a[q[i]]);
}

vector<point> dp[N][N],tmp[N];
int sz[N],n,_;
ll w[N],val[N];
VI e[N];

void dfs(int u,int f) {
	sz[u]=0;
	dp[u][0].pb(point(w[u],w[u]*w[u]));
	for (auto v:e[u]) if (v!=f) {
		dfs(v,u);
		for (int i=0;i<=sz[u]+sz[v];i++) tmp[i].clear();
		for (int i=0;i<=sz[u];i++) for (int j=0;j<=sz[v];j++) {
			vector<point> tt;
			for (auto p:dp[u][i]) for (auto q:dp[v][j]) {
				tt.push_back(point(p.x+q.x,p.y+q.y+2*p.x*q.x));
			}
			rebuild(tt,tt);
			for (auto p:tt) tmp[i+j].pb(p);
		}
		for (int i=0;i<=sz[u]+sz[v];i++) rebuild(tmp[i],dp[u][i]);
		sz[u]+=sz[v];
	}
	for (int i=0;i<=sz[u];i++) {
		val[i]=1ll<<60;
		for (auto p:dp[u][i]) {
			val[i]=min(val[i],p.y);
		}
		dp[u][i+1].insert(dp[u][i+1].begin(),point(0,val[i]));
	}
	sz[u]+=1;
}

void solve() {
	scanf("%d",&n);
	for (int i=1;i<=n;i++) {
		e[i].clear();
		scanf("%lld",w+i);
	}
	rep(i,1,n+1) rep(j,0,n+1) {
		vector<point> p;
		dp[i][j].swap(p);
	}
	rep(i,1,n) {
		int u,v;
		scanf("%d%d",&u,&v);
		e[u].pb(v);
		e[v].pb(u);
	}
	dfs(1,0);
	rep(i,1,n+1) printf("%lld%c",dp[1][i][0].y," \n"[i==n]);
}

int main() {
	for (scanf("%d",&_);_;_--) {
		solve();
	}
}