using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("7");
WillReturn.Add("1 2 5 5 2 3 1");
WillReturn.Add("2");
WillReturn.Add("3 5");
WillReturn.Add("4 6");
//3
//5
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] AArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int UB = AArr.GetUpperBound(0);
var InsSparseTable = new SparseTable<int>(AArr, false);
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
foreach (string EachStr in InputList.Skip(3)) {
SplitAct(EachStr);
int L = wkArr[0] - 1;
int R = wkArr[1] - 1;
var AnswerKouho = new List<int>();
if (L > 0) {
int RangeSta = 0;
int RangeEnd = L - 1;
int MaxVal = InsSparseTable.Query(RangeSta, RangeEnd);
AnswerKouho.Add(MaxVal);
}
if (R < UB) {
int RangeSta = R + 1;
int RangeEnd = UB;
int MaxVal = InsSparseTable.Query(RangeSta, RangeEnd);
AnswerKouho.Add(MaxVal);
}
Console.WriteLine(AnswerKouho.Max());
}
}
}
#region SparseTable
// スパーステーブル
internal class SparseTable<Type>
{
private Type[] mInitArr;
private int UB_0;
private int UB_1;
// 最小値か最大値[開始Ind,2の指数]なArr
private Type[,] mMinOrMaxArr;
// Log2の値[2べきな値] なDict
static Dictionary<int, int> mLogDict = new Dictionary<int, int>();
// 最小値を取得するか?
private bool mIsMin = false;
// コンストラクタ
internal SparseTable(IEnumerable<Type> pInitEnum, bool pIsMin)
{
mIsMin = pIsMin;
mInitArr = pInitEnum.ToArray();
UB_0 = mInitArr.GetUpperBound(0);
int Sisuu = 0;
int Beki2 = 1;
while (true) {
mLogDict[Beki2] = Sisuu;
if (Beki2 > mInitArr.Length) {
break;
}
Beki2 *= 2;
Sisuu++;
}
UB_1 = Sisuu;
mMinOrMaxArr = new Type[UB_0 + 1, UB_1 + 1];
// 長さ1の分を初期化
for (int I = 0; I <= UB_0; I++) {
mMinOrMaxArr[I, 0] = mInitArr[I];
}
for (int LoopLength = 2; LoopLength < int.MaxValue; LoopLength *= 2) {
bool WillBreak = true;
int HalfRange = LoopLength / 2;
for (int I = 0; I <= UB_0; I++) {
int StaInd1 = I;
int EndInd1 = I + HalfRange - 1;
int StaInd2 = EndInd1 + 1;
int EndInd2 = StaInd2 + HalfRange - 1;
if (EndInd2 > UB_0) break;
var KouhoList = new List<Type>();
KouhoList.Add(mMinOrMaxArr[I, mLogDict[HalfRange]]);
KouhoList.Add(mMinOrMaxArr[StaInd2, mLogDict[HalfRange]]);
if (mIsMin) {
mMinOrMaxArr[I, mLogDict[LoopLength]] = KouhoList.Min();
}
else {
mMinOrMaxArr[I, mLogDict[LoopLength]] = KouhoList.Max();
}
WillBreak = false;
}
if (WillBreak) break;
}
}
// 閉区間 [L,R]の最小値または最大値を求める
internal Type Query(int pL, int pR)
{
// 区間を内包する2べきを返す
int Beki2 = 1;
int Sisuu = 0;
while (true) {
int LeftRangeMax = pL + Beki2 - 1;
int RightRangeMin = pR - Beki2 + 1;
if (LeftRangeMax + 1 >= RightRangeMin) {
break;
}
Beki2 *= 2;
Sisuu++;
}
var KouhoList = new List<Type>();
KouhoList.Add(mMinOrMaxArr[pL, Sisuu]);
KouhoList.Add(mMinOrMaxArr[pR - Beki2 + 1, Sisuu]);
if (mIsMin) {
return KouhoList.Min();
}
return KouhoList.Max();
}
}
#endregion