Portals
InterviewTime limit1sMemory limit1024 MB
Each portal leads to exactly one destination, and for each query the task is to count the jumps from start s to end e, or report -1 if e is never reached.
Problem
The new company Sveriges Portaltrafik wants to change how Swedish commuters travel around the country. The company has proposed building a portal system in Sweden that uses fuel-efficient portals to replace trains, planes, and buses. The government has been asked to review the proposal in detail and has asked Swedish elite programmers for help. That is where you come in.
The portal system consists of portals placed at various locations in the country. Each portal has a destination portal . This means that each time you enter , you come out of . Entering does not necessarily bring you back to .
For a number of given start and end portals and , the government wants to know how many times you need to enter the portal you emerge from to get from to , or whether it is possible at all. For each query, answer with the number of times you need to enter portals to get from to . If it is impossible, answer .
Input
The first line contains the integer , the number of portals. The next lines each contain an integer , meaning that portal leads to portal . Portals are numbered from 1 to , and they appear in the input in that order.
The next line contains the integer , the number of queries. The following lines each contain two integers , giving a start portal and an end portal . For every query, .
Output
Print lines, one answer per query: the number of times you need to pass through portals to get from the start portal to the end portal. If it is impossible, print . Print each answer on its own line.
Constraints
Hint

The figure shows the network of the sample input. There are three queries. The first asks how many times you need to enter portals when you start at 1 and want to reach 2. Entering 1 takes you directly to 2, so the answer is 1. Getting from 1 to 4 takes three jumps. The answer to the last query is , because starting from 2 you can never reach 5, no matter how many times you pass through portals.