This page is still under construction.

Parts of this page are still being built. What you see may change.

Joining Couples

Time limit1sMemory limit128 MB

Summary
Each city has one directed outbound flight, forming a functional graph; for each query find the minimum combined distance from two starting cities to any common reachable city, or -1.
Level

Hard8 of 10

Topics
Graph, Tree, Binary search, Prefix sum
Solved
No attempts yet

Problem

Nlogonia's air-traffic rules require every city to register exactly one outbound flight to another city. A flight may be used only in its registered direction: a registered flight from city XX to city YY does not imply a flight from YY to XX. Because every city registers exactly one outbound flight, the total number of registered flights equals the number of cities.

The Association for Couple Matching runs a service that computes the minimum total number of flights a couple must take in order to meet, possibly in a city where neither of them lives. If the two people start in cities AA and BB, the service looks for a city CC that is reachable by air from both AA and BB and minimizes the sum of the number of flights needed to go from AA to CC and the number of flights needed to go from BB to CC. City CC may be equal to AA, to BB, or to both.

You are given the list of all registered flights together with several queries, each giving the two cities where the members of a couple live. For each query, compute the minimum total number of flights the couple needs in order to meet.

Input

The input contains several test cases and ends at end of file.

Each test case is described on several lines:

  • The first line contains an integer NN, the number of cities (2≤N≤1052 \le N \le 10^5). Cities are numbered from 11 to NN.
  • The second line contains NN integers F1,F2,…,FNF_1, F_2, \ldots, F_N, where FiF_i is the city that the single outbound flight registered from city ii goes to (1≤Fi≤N1 \le F_i \le N and Fi≠iF_i \ne i).
  • The third line contains an integer QQ, the number of queries (1≤Q≤1051 \le Q \le 10^5).
  • Each of the next QQ lines contains two integers AA and BB, the cities where the members of one couple live (1≤A,B≤N1 \le A, B \le N).

Within a single test case, whenever it is possible to travel by air from a city XX to a city YY, the number of flights needed to do so is at most 10410^4.

Output

For each query, output one line. If the couple can meet by air travel, print the minimum total number of flights they must take to meet; if they can never meet, print −1-1. Print the answers for all test cases, in order.

Examples4

  1. Example 1

    Input
    3
    2 1 2
    3
    1 2
    1 3
    1 1
    7
    2 1 4 5 3 5 6
    5
    1 3
    4 7
    7 4
    6 2
    2 1
    
    Expected output
    1
    2
    0
    -1
    3
    3
    -1
    1
    
  2. Example 2

    Input
    2
    2 1
    4
    1 1
    1 2
    2 1
    2 2
    
    Expected output
    0
    1
    1
    0
    
  3. Example 3

    Input
    4
    2 1 4 3
    4
    1 3
    1 2
    3 4
    2 4
    
    Expected output
    -1
    1
    1
    -1
    
  4. Example 4

    Input
    7
    2 3 4 5 3 2 6
    4
    1 6
    7 1
    7 4
    6 6
    
    Expected output
    2
    3
    4
    0