import Data.Ord
import Data.List

main = interact $ init . unlines . map (show' . make_purse_light) . read' . lines
 where
  read' ("0":_) = []
  read' (m:coins:xs) = (read m, (map read . words) coins) : read' xs
  show' = unlines . filter ((/='0') . last) . zipWith (++) ["10 ","50 ","100 ","500 "] . map show

make_purse_light (m,coins) = zipWith (\c r -> max 0 (c-r)) coins (rest (wallet-m))
 where
  wallet = sum $ zipWith (*) [10,50,100,500] coins
  rest w' = snd $ foldr change (w',[]) [10,50,100,500]
  change c (m,acc) = let (m',n) = divMod m c in (n,m':acc)