#SGM0000065. Truy vấn k (K-query)

Truy vấn k (K-query)

K-query

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem

You are given an array a1,a2,…,ana_1,a_2,\ldots,a_n. Each query contains i,j,ki,j,k. Count how many values in ai,…,aja_i,\ldots,a_j are strictly greater than kk.

Input

The first line contains nn. The second line contains the array. The third line contains qq. Each of the next qq lines contains i,j,ki,j,k.

Output

For each query, print the number of values greater than kk in [i,j][i,j].

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≤300001\le n\le 30000
  • 1≤q≤2000001\le q\le 200000
  • 1≤ai,k≤1091\le a_i,k\le 10^9
  • 1≤i≤j≤n1\le i\le j\le n

Example

Input

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

Output

2
0
3

Explanation

For 2 4 1, the segment is [1,2,3], and two values are greater than 11.