print@{(@a=sort{@$a-@$b}map[sort/./g],split)[s/-|(\S)(.*?)(\S*\1\S*)/$3$2/g&&redo]},$",@{pop@a},$/for<>