# -*- coding: utf-8 -*-

import sys

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        lines.append(input())
        token = lines[0].split()
        n = int(token[0])
        for i in range(n):
            lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
        token = lines[0].split()
        n = int(token[0])

    a = []
    for i in range(1, n+1):
        a.append(int(lines[i]))

    return (n, a)


def solve(n, a):


    result = []
    x = -1
    y = a[0]
    z = max(a[1:])
    w = z
    for i in range(n):
        b = a[:i]
        c = [a[i]]
        d = a[i+1:]
        if i == 0:
            pass
        elif i == 1:
            x = a[0]
            y = a[1]
            if a[1] < w:
                z = w
            else:
                z = max(a[2:])
            w = max(x, z)
        else:
            x = max(a[i], x)
            y = a[i]
            if a[i] < w:
                z = w
            else:
                if d:
                    z = max(d)
                else:
                    z = -1
            w = max(x, z)
        
        print(w)


def main():
    # 出力

    solve(*parse_input())

if __name__ == '__main__':

    main()
	