#include #include using namespace std; using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; const int INF = 1e9; //const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } // 区間変更・区間和取得 struct S { long long value; int size; }; using F = long long; const F ID = 8e18; S op(S a, S b) { return { a.value + b.value, a.size + b.size }; } S e() { return { 0, 0 }; } S mapping(F f, S x) { if (f != ID) x.value = f * x.size; return x; } F composition(F f, F g) { return (f == ID ? g : f); } F id() { return ID; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector>to(n); rep(i, n - 1) { int u, v; cin >> u >> v; to[u].push_back(v); to[v].push_back(u); } vectoridx(n, -1);//org->trans vectorrev(n, -1);//trans->org vectorpar(n, -1);//trans vector

child(n, { INF,-INF });//trans vector

child2(n, { INF,-INF });//trans { queueq;// trans int cnt = 0; q.push(cnt); idx[0] = cnt; rev[cnt] = 0; while (!q.empty()) { auto top = q.front(); q.pop(); rep(i, to[rev[top]].size()) { int org = to[rev[top]][i]; int trans = idx[org]; if (-1 != trans)continue; cnt++; trans = cnt; idx[org] = trans; rev[trans] = org; q.push(trans); chmin(child[top].first, trans); chmax(child[top].second, trans); par[trans] = top; int p = par[top]; if (-1 != p) { chmin(child2[p].first, cnt); chmax(child2[p].second, cnt); } } } /*rep(i, n) { cout << i << " " << idx[i] << endl; cout << child[idx[i]].first << " " << child[idx[i]].second << endl; cout << child2[idx[i]].first << " " << child2[idx[i]].second << endl; cout << endl; }*/ } vector v(n, { 0, 1 }); rep(i, n) { int a; cin >> a; v[idx[i]].value = a; } lazy_segtree seg(v); int q; cin >> q; while (q--) { int x; cin >> x; x = idx[x]; long long val = 0; {//自分 auto get = seg.get(x); val += get.value; seg.set(x, { 0,1 }); } { int p = par[x]; rep(_, 2) {//親たち if (-1 == p)break; auto get = seg.get(p); val += get.value; seg.set(p, { 0,1 }); if (1 == _)break; if (INF != child[p].first) { get = seg.prod(child[p].first, child[p].second + 1); val += get.value; seg.apply(child[p].first, child[p].second + 1, 0); } p = par[p]; } } if (INF != child[x].first) { auto get = seg.prod(child[x].first, child[x].second + 1); val += get.value; seg.apply(child[x].first, child[x].second + 1, 0); } if (INF != child2[x].first) { auto get = seg.prod(child2[x].first, child2[x].second + 1); val += get.value; seg.apply(child2[x].first, child2[x].second + 1, 0); } cout << val << endl; seg.set(x, { val,1 }); } return 0; }