408980: GYM103401 I Broken routers
Description
There are $$$n$$$ routers in a network each of which has some packets stored in it and a routing table. Routers look up ip address of the packet in its routing table and send the packet to corresponding router neighbor. If the packet's ip address is not in the routing table then the packet will be send to its default router neighbor.
Unfortunately, all of the $$$n$$$ routers have broken down and their routing table become empty. So they can only send packets to their default router neighbor.
There are $$$a_i$$$ packets stored in $$$ith$$$ router at time slot 0. Each of the $$$n$$$ routers send all the packets stored in it to default router neighbor and remove these packets from itself in every time slot. Routers also store the packets it recieved in each time slot.
As a administrator of the network, you want to know how many packets are there in router $$$x$$$ at time $$$t$$$.
InputThe first line contains an integer $$$n(1 \leq n \leq 10^5)$$$, meaning the number of routers.
The second line contains $$$n$$$ intergers $$$a_1,...,a_n(1 \leq a_i \leq 10^9)$$$.
The third line contains $$$n$$$ intergers $$$neb_1,...,neb_n(1 \leq neb_i \leq n)$$$ where $$$neb_i$$$ represents the default router neighbor of $$$ith$$$ router.
The next line contains an integer $$$Q(1 \leq Q \leq 10^5)$$$, meaning the number of queries.
The next $$$Q$$$ lines each contains two integer $$$x,t(1 \leq x \leq n, 0 \leq t \leq 10^9)$$$, having the same meaning in description.
OutputThere are $$$Q$$$ lines in the output, the $$$ith$$$ line contains one interger representing the answer of $$$ith$$$ query.
ExampleInput7 1 2 3 4 5 6 7 2 3 1 1 4 2 2 5 1 2 4 1 5 0 3 3 6 1Output
7 5 5 7 0