import sys from sys import stdin from collections import defaultdict def main(): sys.setrecursionlimit(1 << 25) input = sys.stdin.read().split() ptr = 0 N = int(input[ptr]) ptr += 1 adj = defaultdict(set) for _ in range(N-1): u = int(input[ptr]) v = int(input[ptr+1]) adj[u].add(v) adj[v].add(u) ptr += 2 A = list(map(int, input[ptr:ptr+N])) ptr += N Q = int(input[ptr]) ptr += 1 queries = list(map(int, input[ptr:ptr+Q])) active = A.copy() is_active = [True] * N for x in queries: sum_val = 0 if is_active[x]: sum_val = active[x] is_active[x] = False # Temporarily deactivate to avoid processing x in the loops # Process distance 1 and 2 nodes for y in adj[x]: if is_active[y]: sum_val += active[y] is_active[y] = False # Check neighbors of y (distance 2) for z in adj[y]: if z != x and z not in adj[x]: if is_active[z]: sum_val += active[z] is_active[z] = False # Update x's active value and status active[x] = sum_val is_active[x] = True print(sum_val) if __name__ == '__main__': main()