#include int ri() { int n; scanf("%d", &n); return n; } struct SegTree { int n; std::vector sum; std::vector lazy0; // make 0 SegTree (int n_) { for (n = 1; n < n_; n <<= 1); sum.resize(2 * n); lazy0.resize(2 * n); } void flush(int i) { if (lazy0[i]) { sum[i] = 0; if (i < n) { lazy0[i << 1] = true; lazy0[i << 1 | 1] = true; } lazy0[i] = false; } } void fetch(int node) { sum[node] = sum[node << 1] + sum[node << 1 | 1]; } void set(int i, int64_t val, int node = 1, int node_l = 0, int node_r = 0) { if (!node_r) node_r = n; flush(node); if (node_l == i && node_r == i + 1) sum[node] = val; else { int mid = node_l + ((node_r - node_l) >> 1); if (i < mid) set(i, val, node << 1, node_l, mid); else set(i, val, node << 1 | 1, mid, node_r); fetch(node); } } int64_t sum0(int l, int r, int node = 1, int node_l = 0, int node_r = 0) { if (!node_r) node_r = n; flush(node); if (l <= node_l && r >= node_r) { int64_t res = sum[node]; lazy0[node] = true; flush(node); return res; } if (l >= node_r || r <= node_l) return 0; int mid = node_l + ((node_r - node_l) >> 1); int64_t res = sum0(l, r, node << 1, node_l, mid) + sum0(l, r, node << 1 | 1, mid, node_r); fetch(node); return res; } }; int main() { int n = ri(); std::vector hen[n]; for (int i = 0; i + 1 < n; i++) { int a = ri(), b = ri(); hen[a].push_back(b); hen[b].push_back(a); } std::queue que; que.push(0); int cnt = 0; std::vector ord(n, -1); std::vector par(n, -1); while (que.size()) { int i = que.front(); que.pop(); ord[i] = cnt++; for (auto j : hen[i]) if (ord[j] == -1) que.push(j), par[j] = i; } std::vector > child(n, {1000000000, -1000000000}); std::vector > child2(n, {1000000000, -1000000000}); for (int i = 1; i < n; i++) { child[par[i]].first = std::min(child[par[i]].first, ord[i]); child[par[i]].second = std::max(child[par[i]].second, ord[i]); if (par[i]) { child2[par[par[i]]].first = std::min(child2[par[par[i]]].first, ord[i]); child2[par[par[i]]].second = std::max(child2[par[par[i]]].second, ord[i]); } } SegTree tree(n); for (int i = 0; i < n; i++) tree.set(ord[i], ri()); int q = ri(); for (int i = 0; i < q; i++) { int x = ri(); int64_t num = 0; if (x) { num += tree.sum0(ord[par[x]], ord[par[x]] + 1); num += tree.sum0(child[par[x]].first, child[par[x]].second + 1); } else num += tree.sum0(ord[x], ord[x] + 1); if (x && par[x]) num += tree.sum0(ord[par[par[x]]], ord[par[par[x]]] + 1); if (child[x].first != 1000000000) num += tree.sum0(child[x].first, child[x].second + 1); if (child2[x].first != 1000000000) num += tree.sum0(child2[x].first, child2[x].second + 1); tree.set(ord[x], num); std::cout << num << std::endl; } return 0; }