def ascan; gets.split.map(&:to_i); end strs_t = gets.to_i.times.map{gets.chomp} best = 'z'*250 400.times do ans = '' strs = strs_t.map{|l| l.chars} while !strs.empty? item, i = strs.each_with_index.min{|a,b| a[0][0] == b[0][0] ? rand(-1..1) : a[0][0] <=> b[0][0]} ans << strs[i].shift strs.delete_at(i) if strs[i].empty? end best = [ans,best].min end puts best