Under construction

Still being built. Things may change

달나라에 사는 토끼와 우주에서 떨어지는 떡

Time limit1sMemory limit1024 MB

Summary
Rabbits on a directed functional graph walk shortest paths to each falling rice cake's vertex, and the task is to find each rabbit's total jump count.
Level

Hard8 of 10

Topics
Tree, Graph, Shortest path, DFS
Solved
No attempts yet

문제

There are NN rabbits living on the Moon. The Moon is a graph made of NN vertices and NN directed edges. Initially, rabbit ii is at vertex ii. Each vertex ii has exactly one vertex xix_i that it can reach directly without passing through another vertex. The edge from vertex ii to vertex xix_i has length did_i.

The rabbits on the Moon love rice cakes. Normally a rabbit does not leave its vertex, but when a rice cake falls from space, every rabbit moves very eagerly. MM rice cakes fall in order. The ii-th rice cake falls on vertex viv_i. Every rabbit wants to move to the vertex where the cake falls, but edges have directions, so only rabbits that can reach vertex viv_i move there. A rabbit that cannot reach vertex viv_i stays where it is.

A rabbit always follows a shortest path so that it reaches the vertex as fast as possible. Even when two paths have the same length, the number of jumps needed can differ between rabbits. For rabbit ii, moving a distance of 11 requires rir_i jumps. If rabbit ii moves along a path of length ll, it makes rilr_i l jumps.

The process repeats: a rice cake falls, the rabbits move, and each rabbit that reached the cake's vertex waits there until the next cake falls. The next cake falls only after the last rabbit to arrive has finished moving. When no cakes remain, the process ends.

Let aia_i be the total number of jumps rabbit ii makes from the moment the first rice cake falls until the last cake falls and all movement is finished. Find a1,a2,⋯ ,aNa_1, a_2, \cdots, a_N.

Input

The first line contains NN. (2≤N≤2×105)(2\le N\le 2\times 10^5)

Each of the next NN lines contains xix_i and did_i, separated by a space. (1≤xi≤N;(1\le x_i\le N; i≠xi;i\neq x_i; 1≤di≤105)1\le d_i\le 10^5)

Line N+2N+2 contains r1,r2,⋯ ,rNr_1, r_2, \cdots, r_N, separated by spaces. (1≤ri≤103)(1\le r_i\le 10^3)

Line N+3N+3 contains MM. (1≤M≤2×105)(1\le M\le 2\times 10^5)

Line N+4N+4 contains v1,v2,⋯ ,vMv_1, v_2, \cdots, v_M, separated by spaces. (1≤vi≤N)(1\le v_i\le N)

All numbers in the input are integers.

Output

Print aia_i on the ii-th line for ii from 11 to NN.

Examples1

  1. Example 1

    Input
    7
    2 7
    3 4
    4 2
    5 6
    3 1
    7 5
    6 3
    2 1 3 1 3 2 1
    5
    2 3 1 5 6
    
    Expected output
    38
    12
    24
    15
    27
    0
    3