program zht;
var
i,n,t,q:longint;
z,x:array[1..1000000] of longint;
begin
assign(input,'allm.in');
assign(output,'allm.out');
reset(input);
rewrite(output);
readln(n);
for i:=1 to n do
begin
q:=0;
read(z[i]);
if i=1 then begin write('0 ');x[1]:=0;end else begin
t:=i;
while t<>0 do
begin
t:=t-1;
if z[t]<z[i] then begin write(z[t],' ');x[i]:=z[t];q:=1;break;end;
if z[t]>z[i] then if x[t]<z[i] then begin write(x[t],' ');q:=1;x[i]:=x[t];break;end;
end;
if q=0 then write('0 ');
end;
end;
close(input);
close(output);
end.