main :: IO ()
main = do
  n <- readLn
  if n == 0
    then return ()
    else do
      ls <- getLine
      let ls' = map (read :: String -> Int) (words ls)
          l = concat (repeat ls')
      game 32 l
      main

game :: Int -> [Int] -> IO ()
game i l = do
  let n1 = i - (mod (i - 1) 5)
      n2 = max (n1 - (head l)) 0
  putStrLn $ show n1
  putStrLn $ show n2
  if n2 == 0
    then return ()
    else game n2 (tail l)