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
#if defined(EMBE_DEBUG) && !defined(NDEBUG)
#include "embe-debug.hpp"
#else
#define LOG_INDENT(...) do {} while (false)
#define LOG(...) do {} while (false)
#define DUMP(...) do {} while (false)
#endif

#include <iostream>
#include <iterator>
#include <map>
#include <vector>
using namespace std;

namespace {

long long sqr(int w)
{
  return static_cast<long long>(w) * w;
}

class State {
  int n;
  vector<map<int, long long>> current;

public:
  State(int n_): n{n_}, current(n + 1)
  {
  }

  void insert(int k, int r, long long s)
  {
    auto& cur = current[k];
    auto s2 = sqr(r) + s;
    auto it = cur.upper_bound(r);
    if (it != cur.begin()) {
      --it;
      if (sqr(it->first) + it->second <= s2) return;
    }
    it = cur.insert_or_assign(it, r, s);
    auto it2 = next(it);
    while (it2 != cur.end() && sqr(it2->first) + it2->second >= s2) {
      it2 = cur.erase(it2);
    }
  }

  long long get(int k) const
  {
    return current[k].find(0)->second;
  }

  void propagate()
  {
    ++n;
    current.emplace_back();
    for (int k = 0; k < n; ++k) {
      auto const& [r, s] = *current[k].rbegin();
      insert(k + 1, 0, sqr(r) + s);
    }
  }

  friend State merge(State const& a, State const& b)
  {
    State res(a.n + b.n);
    for (int i = 0; i <= a.n; ++i) {
      for (int j = 0; j <= b.n; ++j) {
        for (auto const& [ar, as]: a.current[i]) {
          for (auto const& [br, bs]: b.current[j]) {
            res.insert(i + j, ar + br, as + bs);
          }
        }
      }
    }
    return res;
  }
};

class Solver {
  int n;

  vector<int> weight;
  vector<vector<int>> neigh;

  State calculate(int v, int p)
  {
    State res(0);
    res.insert(0, weight[v], 0);
    for (int w: neigh[v]) {
      if (w == p) continue;
      res = merge(res, calculate(w, v));
    }

    res.propagate();

    return res;
  }

public:
  explicit Solver(int n_): n{n_}, weight(n), neigh(n)
  {
  }

  void set_weight(int v, int w)
  {
    weight[v] = w;
  }

  void add_edge(int u, int v)
  {
    neigh[u].emplace_back(v);
    neigh[v].emplace_back(u);
  }

  vector<long long> solve()
  {
    int r = 0;
    for (int i = 1; i < n; ++i) {
      if (neigh[i].size() > neigh[r].size()) r = i;
    }
    auto tmp = calculate(r, -1);
    vector<long long> res(n);
    for (int i = 0; i < n; ++i) {
      res[i] = tmp.get(i + 1);
    }
    return res;
  }
};

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int t;
  cin >> t;

  while (t > 0) {
    --t;
    int n;
    cin >> n;
    Solver solver(n);
    for (int i = 0; i < n; ++i) {
      int w;
      cin >> w;
      solver.set_weight(i, w);
    }
    for (int i = 1; i < n; ++i) {
      int a, b;
      cin >> a >> b;
      solver.add_edge(a - 1, b - 1);
    }
    auto res = solver.solve();
    bool first = true;
    for (auto const& x: res) {
      if (first) first = false;
      else cout << ' ';
      cout << x;
    }
    cout << endl;
  }

  return 0;
}