The University of Waterloo is in major disarray. There are too many geese. The sheer amount of geese has made navigating around the campus nearly impossible. That's why Joe is developing a sophisticated piece of software that can help students navigate the campus!
The campus is represented by a cluster of islands, each with one single-directional bridge that goes to another island. On each of these islands, stands a goose. Initially, each island will be occupied by a goose, but after seconds, the goose on the island will fly away into the horizon.
Every morning, each student starts on island and walks precisely islands. It can be proven that there exists only one path that fits this description. Joe's software must be designed to help students navigate across these islands without encountering any geese. It must calculate the earliest time a student should start walking while avoiding all geese on their way. Note, a student walks strictly across one bridge per second, thus the software is able to optimize the starting time by including the time it takes for the student to walk to any given island. Once a student starts walking, they cannot stop to wait for a goose to fly away. It is acceptable for a goose will fly away during or before the moment the student walks on the island.
Since the students are curious about this new software, all of them have submitted a query! Joe, however, does not know how to handle this many queries, so he asked you to help him!
Input Specification
The first line of input contains two integers and .
The next line will contain space-separated integers , representing the time for the goose on island to take off.
On the next line, there will again contain space-separated integers , representing the island that the outgoing bridge leads to.
The following lines will each contain a query of and , representing the number of bridges they have to cross and the starting bridge, respectively.
Output Specification
For each of the queries, output the minimum time the student must wait in order for them to walk without encountering any geese along the way. Assume that each query starts at the second.
Subtasks
Subtask 1 [5%]
Subtask 2 [95%]
No further restrictions
Sample Input
8 4
0 1 2 3 4 9 11 3
2 3 4 5 2 7 6 5
1000 1
4 2
4 8
1 6
Sample Output
0
1
3
10
Explanation
The nodes are representing the islands, and are shown in a node:t_i
format
Query 1: The student doesn't have to wait to start. The goose on the island has flown away at the second, (and similarly for islands when the student reach them) thus the time to wait is .
Query 2: Similar to query , the geese flew off before the student reaches the islands. However, they have to first wait second for the goose on island to clear off.
Query 3: The student must wait seconds to start on island , and then afterwards, all the geese have either already flown off, or flew off when they reached the island.
Query 4: The student starts on island , but needs to wait 9 seconds to walk on it. Then, afterwards, they need to wait an additional second for the goose on island to take off when they reach it. This totals to seconds.
Comments