def data_setting(): N = int(input()) tree = [] tree_line = [list(map(int,input().split())) for i in range(N-1)] for i in range(N): tree_tmp = [] for j in range(len(tree_line)): if tree_line[j][0] == i: tree_tmp.append(tree_line[j][1]) elif tree_line[j][1] == i: tree_tmp.append(tree_line[j][0]) tree.append(tree_tmp) tax = [int(input()) for i in range(N)] M = int(input()) group = [list(map(int,input().split())) for i in range(M)] return M,tree,tax,group def disp(M,tree,tax,group): print(tree) print(tax) print(M) print(group) def route_cal(start): now = start route =[] while now != 0: route.append(now) now = min(tree[now]) route.append(0) print(route) return route def cal(M,tree,tax,group): ans = 0 for i in range(M): print("group",i) A = group[i][0] B = group[i][1] C = group[i][2] routeA = route_cal(A) routeB = route_cal(B) junction = [] for j in range(len(routeA)): if routeA[j] in routeB: junction.append(routeA[j]) P = max(junction) route = routeA[:routeA.index(P)+1] + routeB[:routeB.index(P)] pay_tax = 0 for k in range(len(route)): pay_tax += (tax[route[k]] * C) ans += pay_tax return ans M,tree,tax,group = data_setting() disp(M,tree,tax,group) ans = cal(M,tree,tax,group) print(ans)