object Main extends App {
  val n = io.StdIn.readInt
  val as = (1 to n).map{ _ => io.StdIn.readInt }
  val mxs = as.zipWithIndex
    .foldLeft(None: Option[(Int, Int)], None: Option[(Int, Int)])((ts, i) => {
      val (t1, t2) = ts
      val is = Seq(t1, t2, Some(i)).sorted
      (is(2), is(1))
    })
  val (o1, o2): (Option[(Int, Int)], Option[(Int, Int)]) = mxs
  (0 to n - 1)
    .map(i => {
      (o1, o2) match {
        case (Some(t1), Some(t2)) => if (t1._2 == i) t2._1 else t1._1
      }
    })
    .foreach(println)
}
