Sequence and Queries 6

For each query range [i, j], report the highest number of occurrences of any single value inside that range.

Hard9Segment treeDivide and conquerBinary searchPrefix sumNo attempts yetTime limit2sMemory limit512 MB

Problem

You are given a sequence A1,A2,,ANA_1, A_2, \ldots, A_N of length NN. Write a program that processes the following query on this sequence.

  • i j: print how many times the most frequent value among Ai,Ai+1,,AjA_i, A_{i+1}, \ldots, A_j occurs.

Input

The first line contains the length of the sequence NN (1N1000001 \le N \le 100\,000).

The second line contains A1,A2,,ANA_1, A_2, \ldots, A_N separated by spaces. (1Ai1000001 \le A_i \le 100\,000)

The third line contains the number of queries MM (1M1000001 \le M \le 100\,000).

Each of the next MM lines contains one query ii, jj. (1ijN1 \le i \le j \le N)

Output

Print the answer to each query on its own line, in the order the queries are given.