program C0058;
var n,Q,a,b,i,Cu:longint;
f:array[0..2097152] of longint;
function Con(s:longint):longint;
begin
Con:=0;
while 1<<Con<s+2 do inc(Con);
Con:=1<<Con;
end;
function CMax(a,b:longint):longint;
begin
if a>b then CMax:=a else CMax:=b;
end;
function Getmax(a,b:longint):longint;
begin
inc(a,Cu-1);
inc(b,Cu+1);
Getmax:=-maxlongint;
while a xor b<>1 do begin
if a and 1=0 then Getmax:=CMax(Getmax,f[a+1]);
if b and 1=1 then Getmax:=CMax(Getmax,f[b-1]);
a:=a>>1;
b:=b>>1;
end;
end;
begin
assign(input,'climb.in');
reset(input);
assign(output,'climb.out');
rewrite(output);
readln(n);
inc(n);
Cu:=Con(n);
for i:=1 to n do readln(f[Cu+i]);
for i:=Cu-1 downto 1 do
f[i]:=CMax(f[i<<1],f[i<<1+1]);
readln(Q);
for i:=1 to Q do begin
readln(a,b);
inc(a);
inc(b);
writeln(Getmax(a,b));
end;
close(input);
close(output);
end.