Cannons

No attempts yetTime limit1sMemory limit128 MB

Problem

A certain kingdom is preparing for war. Its army consists mainly of cannons, and the tactics for using them are very simple. The kingdom owns nn cannons. During a battle they are placed one after another along the front line. The cannons are numbered from 11 to nn, from left to right. Each cannon ii has an associated integer pip_i denoting its combat strength.

The Royal Combat Analysis Center (CAB) is responsible for battle tactics. While preparing for war, CAB considers various battle scenarios. A scenario assumes that the front is attacked along the segment from cannon aa to cannon bb. To choose the most effective tactic, CAB needs to know the minimum and maximum combat strength among the cannons on that segment. Help CAB solve this quickly.

Input

The first line contains a natural number dd (1d1001 \le d \le 100), the number of tests. The descriptions of the tests follow one after another.

The first line of each test contains an integer nn (1n1061 \le n \le 10^6), the number of cannons. The second line contains nn integers pip_i (109pi109-10^9 \le p_i \le 10^9), the combat strengths of the cannons. The next line contains an integer mm (1m1051 \le m \le 10^5), the number of scenarios CAB considers. Each of the following mm lines contains one scenario, given as two integers aa and bb (1abn1 \le a \le b \le n).

Output

For each scenario, print the minimum and the maximum combat strength among the cannons on the segment from cannon aa to cannon bb, separated by a single space on one line.