($N,$M,$K)=glob<>; @_=<>; @d=sort{1}glob pop@_; / .* /,push(@{$`},[$&+0,$']),push(@{$&+0},[$`,$'])for@_; sub f{ my($i,$j)=@_; $j==$K||grep@$_[1]==$d[$j]&&f(@$_[0],$j+1),@$i } @g=grep{f$_,0}1..$N; print@g."\n@g"