Under construction

Still being built. Things may change

Elections

Time limit2sMemory limit1024 MB

Summary
Given n candidate votes and m range queries, report a candidate with the highest vote count inside each range [l, r].
Level

Medium7 of 10

Topics
Sliding window, Sorting, Array
Solved
No attempts yet

Problem

Recently the country held an election that used modern technology. For each ballot, it is known when it was put into the box and which candidate the voter chose. After the votes were counted, everyone wanted to analyze how the election went. Why else would the whole system have been introduced?

Many people wanted to know the following: for a given time interval, which candidate received the most votes? If a candidate got many votes in a short interval and fewer outside it, fraud may have happened during that interval. A site was built to answer such questions, but the number of visitors was unexpectedly large, and the site could not keep up with the load.

The management decided to rewrite the site so that it serves mm users at the same time. Your task is to write a program that answers the question users ask: which candidate received the most votes in a given interval?

Input

The first line contains the number of processed ballots nn (1≤n≤100,0001 \leq n \leq 100,000).

The second line contains nn integers aia_i separated by spaces (1≤ai≤1091 \leq a_i \leq 10^9). Here aia_i is the number of the candidate chosen by the voter who put ballot number ii into the box.

The third line contains the number of queries mm (1≤m≤100,0001 \leq m \leq 100,000).

Each of the next mm lines contains a query lil_i rir_i (1≤li≤ri≤n1 \leq l_i \leq r_i \leq n).

Output

For each query, print one number on a separate line: the number of the most popular candidate among the ballots numbered from lil_i to rir_i, inclusive.

A candidate is the most popular if no other candidate received more votes among those ballots. Ties are allowed.

Hint

For the last query of the sample, the answer 1 is also accepted. Ololo, partial solutions.

Examples1

  1. Example 1

    Input
    6
    1 1 2 2 1 1
    3
    1 6
    2 4
    2 5
    
    Expected output
    4
    2
    2