>>911 「cat cat」とか重複するペアとか任意のペアが来てもオッケーなバージョン

def qs(er)
h = Hash.new Float::INFINITY
erc = 0
er.each do |r|
min = r.map{|e| h[e]}.min
ern = min == Float::INFINITY ? erc += 1 : min
r.each{|e| h[e] = ern}
end
h.group_by(&:last).values.map{|s| s.map(&:first).sort}
end

er = $<.readlines.map &:split
puts qs(er).map{|a| a * ' '} * $/
#=>
dove duck goose lark pigeon
cat dog horse rhino
cod eel goldfish squid