m={} 2.times do gets.to_i.times do s = gets.chomp s[0...-1].chars.each_with_index do |c, i| m[c] = s[i+1] end m[s[-1]] ||= nil; end end case m.to_a.map(&:last).count(nil) when 1 c = nil s = "" while m.key(c) do s << m.key(c) c = m.key(c) end puts s.reverse else puts -1 end