#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include using namespace std; #include using namespace atcoder; #define REP(i,n) for(int i = 0; i < (int)n; i++) #define RREP(i,n) for(int i = (int)n-1; i >= 0; i--) #define LREP(i,n) for(LL i = 0; i < (LL)n; i++) #define Vi vector #define Vl vector #define P pair #define LP pair #define T3 tuple #define T4 tuple #define INF 1000000007 #define SIZE 400010 #define MOD 998244353 typedef long long LL; LL op(LL a, LL b) { return a + b; } LL e() { return 0; } LL mapping(LL f, LL x) { return f * x; } LL composition(LL f, LL g) { return f * g; } LL id() { return 1; } int N, Q; Vi E[SIZE]; LL A[SIZE]; int par[SIZE]; int L[SIZE], R[SIZE]; bool used[SIZE]; int position[SIZE]; Vi B; int main() { cin >> N; REP(i, N - 1) { int u, v; cin >> u >> v; E[u].push_back(v); E[v].push_back(u); } REP(i, N) cin >> A[i]; queue que; que.push(0); par[0] = N; while (que.size()) { int u = que.front(); que.pop(); used[u] = true; position[u] = B.size(); B.push_back(u); L[u] = -1, R[u] = -1; REP(i, E[u].size()) { if (E[u][i] == par[u]) { Vi temp; for (int j = E[u].size() - 1; j > i; j--) { temp.push_back(E[u][j]); E[u].pop_back(); } E[u].pop_back(); for (auto e : temp) { E[u].push_back(e); } } } for (auto v : E[u]) { if (L[u] == -1) L[u] = v; R[u] = v; par[v] = u; que.push(v); } } lazy_segtree seg(N); REP(i, N) { seg.set(i, A[B[i]]); } map right; cin >> Q; REP(q, Q) { int x; cin >> x; int u = x; LL res = 0; res += seg.get(position[u]); seg.set(position[u], 0); u = par[u]; res += seg.get(position[u]); seg.set(position[u], 0); u = par[u]; res += seg.get(position[u]); seg.set(position[u], 0); u = par[x]; if (L[u] != -1) { res += seg.prod(position[L[u]], position[R[u]] + 1); seg.apply(position[L[u]], position[R[u]] + 1, 0); } u = x; if (L[u] != -1) { res += seg.prod(position[L[u]], position[R[u]] + 1); seg.apply(position[L[u]], position[R[u]] + 1, 0); } u = L[x]; if (u != -1) { u = L[u]; if (u != -1) { if (right.find(u) == right.end()) { int i = position[u]; for (; i < N; i++) { if (par[par[B[i]]] != x) { break; } } right[u] = B[i - 1]; } res += seg.prod(position[u], position[right[u]] + 1); seg.apply(position[u], position[right[u]] + 1, 0); } } cout << res << endl; seg.set(position[x], res); } }