N = gets.to_i words = N.times.map { gets.chomp }.tally %w(a b).repeated_permutation(N).each do |units| word = units.join if not words[word] puts word exit end end