$_,*m=*$< puts split.map{|x|[x,[0,4,8].map{|p|m.grep(/^.{#{p}}#{x}/).size}]*" "}.sort_by{|y|s=y[4,5];[s.sum,s]}.reverse