import Control.Monad
import Text.Printf
import Data.List

getInts :: IO [Int]
getInts = map read . words <$> getLine

main :: IO()
main = do
  n <- readLn
  as <- replicateM n $ getInts
  let aas = concat as
  printf "%s" $ slv aas

slv ::[Int] -> String
slv [a] = show a
slv (a:as) = slv2 (a:as) max1 max2 []
  where (max1:max2:ret) = reverse $ sort (a:as)

slv2 :: [Int] -> Int -> Int -> [String] -> String
slv2 [] _ _ ret = unlines $ reverse ret
slv2 (a:as) max1 max2 ret | a == max1 = slv2 as max1 max2 ((show max2):ret)
                          | otherwise = slv2 as max1 max2 ((show max1):ret)