#!/usr/bin/perl
use 5.016;
use warnings;
use utf8;
binmode STDOUT => ':encoding(utf8)';
sub f {
my @queue = ([ [], [ (0) x @lists ] ]);
while(@queue){
my ($pattern, $pos) = @{ shift @queue };
if (not grep{ @{$lists[$_]} > $pos->[$_] } (0 .. $#lists)){ foreach(@lists){
my @p = @{$_};
foreach(@{$pattern}){
@p or last;
}
}
}
my %memo;
foreach my $n (0 .. $#lists){
my $x = $lists[$n]->[$pos->[$n]];
$memo{$x} = [ [ @{$pattern}, $x ], [ @{$pos} ] ];
}
$memo{$x}->[1]->[$n]++;
}
}
}
IyEvdXNyL2Jpbi9wZXJsCnVzZSA1LjAxNjsKdXNlIHdhcm5pbmdzOwp1c2UgdXRmODsKYmlubW9kZSBTVERPVVQgPT4gJzplbmNvZGluZyh1dGY4KSc7CgpzdWIgZiB7CglteSBAbGlzdHMgPSBtYXB7IFtzcGxpdCAvL10gfSBAXzsKCglteSBAcXVldWUgPSAoWyBbXSwgWyAoMCkgeCBAbGlzdHMgXSBdKTsKCXdoaWxlKEBxdWV1ZSl7CgkJbXkgKCRwYXR0ZXJuLCAkcG9zKSA9IEB7IHNoaWZ0IEBxdWV1ZSB9OwoKCQlpZiAobm90IGdyZXB7IEB7JGxpc3RzWyRfXX0gPiAkcG9zLT5bJF9dIH0gKDAgLi4gJCNsaXN0cykpewoJCQlmb3JlYWNoKEBsaXN0cyl7CgkJCQlteSBAcCA9IEB7JF99OwoJCQkJZm9yZWFjaChAeyRwYXR0ZXJufSl7CgkJCQkJQHAgb3IgbGFzdDsKCQkJCQlwcmludCgkcFswXSBlcSAkXyA/IHNoaWZ0KEBwKSA6ICfilqEnKTsKCQkJCX0KCQkJCXByaW50ICJcbiI7CgkJCX0KCQkJcmV0dXJuOwoJCX0KCgkJbXkgJW1lbW87CgkJZm9yZWFjaCBteSAkbiAoMCAuLiAkI2xpc3RzKXsKCQkJbXkgJHggPSAkbGlzdHNbJG5dLT5bJHBvcy0+WyRuXV07CgkJCWRlZmluZWQ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