import Data.Maybe
import Control.Applicative
import qualified Data.ByteString.Lazy.Char8 as B

readInt = fst . fromJust . B.readInt

main = do
 os <- map (tail . B.words) . init . B.lines <$> B.getContents
 mapM_ print (process Empty os) 

process _ [] = []
process q ([x]:os) = process (push (readInt x) q) os
process q (_:os) = top q : process (pop q) os


data Heap a = Empty | Node a [Heap a] deriving (Show, Eq)

merge Empty h = h
merge h Empty = h
merge (Node x xs) (Node y ys)
  | x > y     = Node x (Node y ys : xs)
  | otherwise = Node y (Node x xs : ys)

mergeList []       = Empty
mergeList [x]      = x
mergeList (x:y:xs) = merge (merge x y) (mergeList xs)

push x h = merge (Node x []) h

pop (Node x xs) = mergeList xs

top (Node x xs) = x