while true
  price = gets.to_i
  break if price == 0
  coin_numbers = gets.split.map(&:to_i)
  coin_values = [10, 50, 100, 500]
  coins_and_values = coin_numbers.zip(coin_values)
  total_price = coins_and_values.inject(0){ |sum, pair|
    sum += pair[0] * pair[1]
  }
  change = total_price - price
  change_coins = []
  coin_values.reverse.each do |value|
    coin = change / value
    change = change % value
    change_coins.insert(0, coin)
  end
  temp_pairs = coin_numbers.zip(change_coins)
  actual_coins = temp_pairs.map {|pair| pair[0] > pair[1] ? pair[0] - pair[1] : 0}
  coin_values.zip(actual_coins).each do |zip|
    puts "#{zip[0]} #{zip[1]}" if zip[1] > 0
  end
  puts nil
end