PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000078. Những món quà lưu niệm (Souvenirs)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Những món quà lưu niệm (Souvenirs)

Souvenirs

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Each position is a shop with price aia_i. For each interval [l,r][l,r] containing at least two shops, choose two distinct indices minimizing the absolute price difference ∣ai−aj∣|a_i-a_j|. Equal prices yield 0.

Input

Read nn, its prices, qq, then qq ranges with l<rl<r.

Output

Print the minimum price difference for each interval.

Subtasks

  • Subtask 1 (10 points): 1≤n≤401\le n\le 40, 1≤q≤601\le q\le 60. 2≤n2\le n; 0≤ai≤1090\le a_i\le10^9; 1≤l<r≤n1\le l<r\le n.
  • Subtask 2 (20 points): 1≤n≤20001\le n\le 2000, 1≤q≤30001\le q\le 3000. 2≤n2\le n; 0≤ai≤1090\le a_i\le10^9; 1≤l<r≤n1\le l<r\le n.
  • Subtask 3 (25 points): 1≤n≤200001\le n\le 20000, 1≤q≤300001\le q\le 30000. 2≤n2\le n; 0≤ai≤1090\le a_i\le10^9; 1≤l<r≤n1\le l<r\le n.
  • Subtask 4 (45 points): 1≤n≤1000001\le n\le 100000, 1≤q≤3000001\le q\le 300000. 2≤n2\le n; 0≤ai≤1090\le a_i\le10^9; 1≤l<r≤n1\le l<r\le n.

Examples

Input

8
3 1 4 1 5 9 2 6
4
1 8
1 3
4 8
5 7

Output

0
1
1
3

Explanation

In the whole array, two different shops offer price 1, giving 0. In [1,3][1,3], prices 3,1,43,1,4 have minimum difference 1. In [5,7]=[5,9,2][5,7]=[5,9,2], the smallest difference is 3.