#d1205. Closest Equals

Closest Equals

Closest Equals

Problem Description

You are given a sequence aa indexed from 11 to nn, and then mm queries.

Each query gives a range [l,r][l, r]; find the minimum distance between two equal elements aia_i and aja_j within the range — that is, find a pair (ai,aj)(a_i, a_j) such that:

  • ai=aja_i = a_j;
  • l≤i,j≤rl \le i, j \le r (i≠ji \neq j);

and minimize ∣i−j∣|i - j|. If no two equal elements exist in the range, output −1-1.

Input Format

The first line contains two integers n,mn, m with 1≤n,m≤5×1051 \le n, m \le 5 \times 10^5.

The second line contains nn integers aia_i with 1≤ai≤1091 \le a_i \le 10^9.

Each of the next mm lines contains two integers l,rl, r with 1≤l≤r≤n1 \le l \le r \le n.

Output Format

Output mm lines, each containing one integer: the answer to the query.

Sample Input 1

5 3
1 1 2 3 2
1 5
2 4
3 5

Sample Output 1

1
-1
2

Sample Input 2

6 5
1 2 1 3 2 3
4 6
1 3
2 5
2 4
1 6

Sample Output 2

2
2
3
-1
2