Intrinsic Interval

For each query range in a permutation, find the smallest subarray containing it whose values form a set of consecutive integers.

Hard9Segment treeStackPrefix sumNo attempts yetTime limit3sMemory limit512 MB

Problem

You are given a permutation π\pi of the integers 11 through nn. For indices aa and bb with 1abn1 \le a \le b \le n, the consecutive subsequence πab=(πa,πa+1,,πb)\pi_{ab} = (\pi_a, \pi_{a+1}, \dots, \pi_b) is an interval if sorting it yields a run of consecutive integers. For example, in the permutation π=(3,1,7,5,6,4,2)\pi = (3, 1, 7, 5, 6, 4, 2) the subsequence π36\pi_{36} is an interval because it holds the numbers 44 through 77, while π13\pi_{13} is not.

The intrinsic interval of a subsequence πxy\pi_{xy} is the shortest interval πab\pi_{ab} that contains it, so axyba \le x \le y \le b. The length of an interval is the number of elements in it. Such a shortest interval is always unique. The intersection of two overlapping intervals is again an interval, so the intersection of all intervals containing πxy\pi_{xy} is itself an interval, and that intersection is the intrinsic interval.

You are given π\pi and mm of its subsequences. Find the intrinsic interval of each subsequence.

Input

The first line contains the size nn (1n1000001 \le n \le 100000) of the permutation π\pi. The second line contains nn different integers π1,π2,,πn\pi_1, \pi_2, \dots, \pi_n (1πjn1 \le \pi_j \le n), the permutation itself.

The third line contains the number of subsequences mm (1m1000001 \le m \le 100000). The jj-th of the following mm lines contains the endpoints xjx_j and yjy_j (1xjyjn1 \le x_j \le y_j \le n) of the jj-th subsequence.

Output

Print mm lines. The jj-th line must contain the endpoints aja_j and bjb_j (1ajbjn1 \le a_j \le b_j \le n) of the intrinsic interval of the subsequence πxjyj\pi_{x_j y_j}, separated by a space.