@@=1..<>;${s/^/ /;map{0while-$$_+($$_=$$$_//$_)}/( \d+)(.*)/;($a,$b)=sort{@$a-@$b||$b-$a}$$1,$$2;$2?@{$$a=$b}[$a-$b&&@$a+@$b]=0:print$$_,$/}for<>,@@