N = int(input()) A = list(map(int, input().split())) P = [0] * (N + 1) pre = [-1] * (N + 1) post = [-1] * (N + 1) ans = [] for i, a in enumerate(A): P[a] = i if i > 0: pre[a] = A[i - 1] if i < N - 1: post[a] = A[i + 1] for i in range(1, N + 1): if P[i] == -1 or post[i] == -1: continue x = P[i] y = post[i] P[i] = -1 P[y] = -1 if pre[i] != -1: post[pre[i]] = post[y] if post[y] != -1: pre[post[y]] = pre[i] ans.append(i) ans.append(y) print(*ans)