Maximum Distance

0

0 votes
Problem

Alice has N integers a1 , a2 , .... , an1 , an . An integer may occur multiple times in the array.

Alice has to answer Q queries, each query consisits of two integers x and y. Alice need to find the maximum value of |ij| such that ai=x and ai=y. Help Alice answer the Q queries.

 

INPUT:

    The first line contains a single integer N.

    The second line contains N  space separated integers.

    The third line contains a single integer Q.

    Q lines follow, each line contains two space sepatated integers x and y.

 

OUTPUT:

    Output Q lines, ith of which line should contain a single integer max|ij| associated with ith query if both x and y are in the array. If either of x or y is not present in the array output -1.

 

CONSTRAINTS:

    2N200000

   0ai109 for all i from 1 to N

Sample Input
10
2 1 3 3 2 1 3 1 2 1
4
1 2
2 3
4 1
1 3
Sample Output
9
6
-1
7
Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?