N=int(input()) P=list(map(int,input().split())) P_INV=[-1]*(N+1) for i in range(N): P_INV[P[i]]=i LEFT=[i-1 for i in range(N)] RIGHT=[i+1 for i in range(N)] def use(i): RIGHT[LEFT[i]]=RIGHT[i] LEFT[RIGHT[i]]=LEFT[i] USE=[0]*N ANS=[] for i in range(1,N+1): if USE[P_INV[i]]==1: continue if P_INV[i]==N-1: continue ANS.append(i) USE[P_INV[i]]=1 x=RIGHT[P_INV[i]] ANS.append(P[x]) use(P_INV[i]) use(x) USE[P_INV[i]]=1 USE[x]=1 print(*ANS)