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

#LCA0000020. Đường đi giữa các khách sạn (Tak and Hotels)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đường đi giữa các khách sạn (Tak and Hotels)

Tak and Hotels

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn hotels at distinct positions x1<x2<⋯<xnx_1<x_2<\cdots<x_n along a straight road. On each day Tak may travel from his current hotel to another hotel at distance at most LL and stay there overnight.

For every query (a,b)(a,b), find the minimum number of days required to travel from hotel aa to hotel bb. Adjacent hotels are at most LL apart, so every trip is feasible.

Travel is permitted in either direction.

Input

  • The first line contains nn.
  • The second line contains hotel positions x1,…,xnx_1,\ldots,x_n.
  • The next line contains LL.
  • The following line contains qq.
  • The last qq lines contain hotel indices a,ba,b.

Output

Print the minimum number of days for each trip, one line per query.

Subtasks

  • Subtask 1 (20 points): n≤30n\le30; q≤60q\le60; 2≤n≤1052\le n\le10^5; 1≤x1<⋯<xn≤1091\le x_1<\cdots<x_n\le10^9; 1≤xi+1−xi≤L≤1091\le x_{i+1}-x_i\le L\le10^9; 1≤q≤1051\le q\le10^5; 1≤aeb≤n1\le a e b\le n.
  • Subtask 2 (30 points): n≤1000n\le1000; q≤3000q\le3000; 2≤n≤1052\le n\le10^5; 1≤x1<⋯<xn≤1091\le x_1<\cdots<x_n\le10^9; 1≤xi+1−xi≤L≤1091\le x_{i+1}-x_i\le L\le10^9; 1≤q≤1051\le q\le10^5; 1≤aeb≤n1\le a e b\le n.
  • Subtask 3 (50 points): 2≤n≤1052\le n\le10^5; 1≤x1<⋯<xn≤1091\le x_1<\cdots<x_n\le10^9; 1≤xi+1−xi≤L≤1091\le x_{i+1}-x_i\le L\le10^9; 1≤q≤1051\le q\le10^5; 1≤aeb≤n1\le a e b\le n.

Examples

Input

9
1 3 6 13 15 18 19 29 31
10
4
1 8
7 3
6 7
8 5

Output

4
2
1
2

Explanation

With L=10L=10, travel from hotel 11 to 88 along 1o3o5o7o81 o3 o5 o7 o8 takes 44 days. Going from hotel 66 to 77 takes just 11 day.