use Set::Object 'set';


sub createindex
{
    my @files = @_;

    my %iindex;

    foreach my $file (@files)
    {
    open(F, "<", $file) or die "Can't read file $file: $!";
    while(<F>) {
            s/\A\W+//;
        foreach my $w (map {lc} grep {length() >= 3} split /\W+/)
        {
        if ( exists($iindex{$w}) )
        {
            $iindex{$w}->insert($file);
        } else {
            $iindex{$w} = set($file);
        }
        }
    }
    close(F);
    }
    return %iindex;
}


sub search_words_with_index
{
    my %idx = %{shift()};
    my @words = @_;
    my $res = set();

    foreach my $w (map {lc} @words)
    {
    $w =~ s/\W+//g;            
        length $w < 3 and next;
        exists $idx{$w} or return set();
        $res = $res->is_null
          ? set(@{$idx{$w}})
          : $res * $idx{$w};       
    }
    return @$res;
}



my @searchwords = split /,/, shift;

print "$_\n"
    foreach search_words_with_index({createindex(@ARGV)}, @searchwords);