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
#include <ctime>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 300;
const int MAXT = 10;
const long long MAXA = 9223372036854775807;

#define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++)




struct value {
  int cuts;
  long long closed;
  long long leftover;
  long long finish() const {
    return closed + leftover*leftover;
  }
  value(int a, long long b, long long c): cuts(a), closed(b), leftover(c) {}

  value operator+(const value& v) const {
    return value(cuts + v.cuts, closed + v.closed, leftover + v.leftover);
  }
  bool better(const value& v) const {
    return (finish() <= v.finish() && leftover <= v.leftover);
  }
};
typedef vector<vector<value> > vec;

long long a[MAXN];
vector<int> L[MAXN];
int order[MAXN];
int position[MAXN];
vec V[MAXN];

void push_better(vector<value> & v, value x) {
  for (int i = 0; i<v.size(); i++) {
    if (x.better(v[i])) {
      swap(v[i], v.back());
      v.pop_back();
      i--;
    } else {
      if (v[i].better(x)) {
        return;
      }
    }
  }
  v.push_back(x);
}


void test() {
  int n;
  cin >> n;
  FOR (i, n) {
    cin >> a[i];
    L[i].clear();
    V[i].clear();
    position[i] = -1;
  }
  FOR (i, n-1) {
    int b, c;
    cin >> b >> c;

    L[b-1].push_back(c-1);
    L[c-1].push_back(b-1);
  }
  order[0] = 0;
  position[0] = 0;
  int head = 1;
  for(int idx = 0; idx<n; idx++) {
    int i = order[idx];
    FOR(jdx, L[i].size()) {
      int j = L[i][jdx];
      if (position[j] == -1) {
        // cout << "ADD " << j<< " (" << i << ")" << endl;
        position[j] = head;
        order[head++] = j;
      }
    }
  }


    // cerr << "n " << n << endl;
  for (int idx = n-1; idx>=0; idx--) {
    int i = order[idx];
    // cerr << "ORD " << i << endl;

    vec v(1);
    v[0].push_back(value(0,0,0));

    FOR(jdx, L[i].size()) {
      int j = L[i][jdx];
      if (position[j] > position[i]) {
        vec & w = V[j];

        vec z;
        z.swap(v);
        v.resize(z.size() + w.size() - 1);

        FOR(zc, z.size()) {
          FOR(zi, z[zc].size()) {
            FOR(wc, w.size()) {
              FOR(wi, w[wc].size()) {
                value x = z[zc][zi] + w[wc][wi];
                push_better(v[x.cuts], x);
              }
            }
          }
        }
      }
    }
    v.resize(v.size() + 1);
    for (int cuts = v.size() - 2; cuts >= 0; cuts--) {
      long long best = MAXA;
      FOR (j, v[cuts].size()) {
        v[cuts][j].leftover += a[i];
        best = min(best, v[cuts][j].finish());
      }
      v[cuts+1].push_back(value(cuts+1, best, 0));
    }

    v.swap(V[i]);
  }

  FOR (i, V[0].size()-1) {
    long long best = MAXA;
    FOR (j, V[0][i].size()) {
      best = min(best, V[0][i][j].finish());
    }
    cout << best << " ";
  }
  cout << endl;
}



int main() {
  ios_base::sync_with_stdio(0);
  int t; cin >> t;
  FOR(i, t) test();
  return 0;
}