import List main=interact$f.sort.map(r.words).lines r s=(0+read(s!!2),s) f x=x#[[y]|y<-x>>=snd] ((_,v):w)#l=case[filter(elem t)l|t<-v]of[p:_,q:_,_]->unwords([head$show$p/=q]:v)++'\n':w#(union p q:l) _#_=[]