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
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

struct Desc {
  int x, y;
  bool removed;
};

typedef long long LL;
typedef vector<int> A;
typedef vector<Desc> B;
typedef vector<vector<pair<int, int>>> E;

vector<bool> visited;

int costt(int v, const A& a, B& b, const E& e) {
  if (visited[v]) return 0;
  visited[v] = true;
  int result = a[v];
  for (pair<int, int> u : e[v]) {
    if (!b[u.second].removed) {
      result += costt(u.first, a, b, e);
    }
  }
  visited[v] = false;
  return result;
}

LL cost(int v, const A& a, B& b, const E& e) {
  LL res = costt(v, a, b, e);
  return res*res;
}

int endcostt(int v, const A& a, B& b, const E& e) {
  if (visited[v]) return 0;
  visited[v] = true;
  int result = a[v];
  for (pair<int, int> u : e[v]) {
    if (!b[u.second].removed) {
      result += endcostt(u.first, a, b, e);
    }
  }
  return result;
}

LL endcost(int v, const A& a, B& b, const E& e) {
  LL res = endcostt(v, a, b, e);
  return res*res;
}

int try_swap(int in, int out, const A& a, B& b, const E& e) {
  LL diff = 0;
  diff += cost(b[in].x, a, b, e);
  diff += cost(b[in].y, a, b, e);
  b[in].removed = false;
  diff -= cost(b[in].x, a, b, e);
  diff += cost(b[out].x, a, b, e);
  b[out].removed = true;
  diff -= cost(b[out].x, a, b, e);
  diff -= cost(b[out].y, a, b, e);
  b[in].removed = true;
  b[out].removed = false;
  return diff;
}

LL solve(int k, int n, const A& a, B& b, const E& e) {
  vector<int> in;
  vector<int> out;
  for (int i=0; i<n-1; ++i) {
    if (i<k) {
      in.push_back(i);
      b[i].removed = true;
    } else {
      out.push_back(i);
      b[i].removed = false;
    }
  }
  visited = vector<bool>(n, false);

  bool retry = true;
  while (retry) {
    retry = false;
    for (int i=0; i<in.size(); ++i) {
      for (int j=0; j<out.size(); ++j) {
        if (try_swap(in[i], out[j], a, b, e) > 0) {
          swap(in[i], out[j]);
          b[in[i]].removed = true;
          b[out[j]].removed = false;
          retry = true;
        }
      }
    }
  }

//  printf("\n");
//  printf("\n");
//  for (int inn : in) {
//    printf("%d - %d\n", b[inn].x + 1, b[inn].y + 1);
//  }

  LL result = 0;
  for (int i=0; i<n; ++i) {
    result += endcost(i, a, b, e);
  }
  return result;
}

void solve() {
  int n; scanf("%d", &n);
  A a(n);
  for (int i=0; i<n; ++i) scanf("%d", &a[i]);
  B b(n-1);
  E e(n);
  for (int i=0; i<n-1; ++i) {
    int x, y; scanf("%d %d", &x, &y);
    e[x-1].emplace_back(y-1, i);
    e[y-1].emplace_back(x-1, i);
    b[i].x = x-1;
    b[i].y = y-1;
  }

  for (int i=0; i<n; ++i) {
    printf("%lld ", solve(i, n, a, b, e));
  }
  printf("\n");
}

int main() {
  int t; scanf("%d", &t);
  for (int i=0; i<t; ++i) solve();
  return 0;
}