for(reverse<>){chomp;$c=0;push@{$b[$c++]}," "ne$_?$_:()for split//}while($_=join"",map{shift(@$_)or" "}@b){last if/^ +$/;unshift@o,$_}$,=" ";print@o