#SGM0000074. Tấm biển trên hàng rào (Sign on Fence)

Tấm biển trên hàng rào (Sign on Fence)

Sign on Fence

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem

Panel ii has height hih_i. For query l,r,wl,r,w, choose exactly ww consecutive panels inside [l,r][l,r]. A rectangular sign can be as high as the minimum panel in that chosen block. Maximize this height.

Input

The first line contains nn, then the heights, then mm, followed by mm queries l,r,wl,r,w.

Output

Print the maximum possible sign height for every query.

Subtasks

Subtask 1 (20%)

  • n≤30n\le 30, number of queries ≤30\le 30.
  • All other conditions are the same as Subtask 3.

Subtask 2 (30%)

  • n≤3000n\le 3000, number of queries ≤3000\le 3000.
  • All other conditions are the same as Subtask 3.

Subtask 3 (50%)

  • 1≤n,m≤1051\le n,m\le10^5
  • 1≤hi≤1091\le h_i\le10^9
  • 1≤l≤r≤n1\le l\le r\le n
  • 1≤w≤r−l+11\le w\le r-l+1

Example

Input

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

Output

2
3
1

Explanation

For 2 5 3, panels 2..4 have heights [2,2,3], so a height-22 sign fits.