>>911 perl
%h;
while(<>){
chomp;
($f, $s) = split/ /;#first, second

undef $keyf;
undef $keys;

for $k (keys %h){
$keyf = $k if grep /$f/, @{$h{$k}};
$keys = $k if grep /$s/, @{$h{$k}};
}

if(!defined $keyf and !defined $keys){
push @{$h{$.}}, $f;
push @{$h{$.}}, $s;
}else{
push @{$h{$keys}}, $f unless defined $keyf;
push @{$h{$keyf}}, $s unless defined $keys;
}
}

map{print join " ", (sort{$a cmp $b}@{$h{$_}}), "\n"}keys %h;