#SGM0000075. Một lần xuất hiện (One Occurrence)

Một lần xuất hiện (One Occurrence)

One Occurrence

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem

For each subarray query [l,r][l,r], print any value that occurs exactly once in that subarray. Print 0 if no such value exists.

Input

The first line contains nn, the second line the array, the third line qq, followed by qq ranges l,rl,r.

Output

For every query print 0 if no unique-occurrence value exists; otherwise print any valid value.

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≤5000n\le 5000, number of queries ≤5000\le 5000.
  • All other conditions are the same as Subtask 3.

Subtask 3 (50%)

  • 1≤n,q≤5⋅1051\le n,q\le5\cdot10^5
  • 1≤ai≤5⋅1051\le a_i\le5\cdot10^5
  • 1≤l≤r≤n1\le l\le r\le n

Example

Input

6
1 1 2 3 2 4
2
2 6
1 2

Output

4
0

Explanation

In sample range [2,6], values 1,3,4 occur once, so 4 is valid. Range [1,2] is [1,1], hence the answer is 0.