
N = int(input())
A_lst = []
max_num = -1
max_index_lst = []
next_num = -1

for i in range(N):
    Ai = int(input())
    A_lst.append(Ai)
    if max_num == -1:
        max_num = Ai
        max_index_lst.append(i)
    elif next_num == -1:
        if max_num > Ai:
            next_num = Ai
        else:
            next_num = max_num
            max_num = Ai

    elif max_num <= Ai:
        if max_num == Ai:
            max_index_lst.append(i)
        else:
            max_index_lst = [i]
        next_num = max_num
        max_num = Ai

    
    elif next_num < Ai:
        next_num = Ai
#print()
ans_lst = [max_num]*N

if len(max_index_lst) == 1:
    ans_lst[max_index_lst[0]] = next_num

for i in ans_lst:
    print(i)