main=interact$unlines.f f(x:y@(z:_))=f y++[x:w|w<-f y,z/=x] f _=[[]]