chomp($n=<>);for(1..$n){<>;chomp($x=<>);chomp($y=<>);$s=(split/ /,$x)[-1];for(split/ /,$y){$d{$_}+=$s}}@l=sort{$d{$b}<=>$d{$a}||$a cmp $b}keys %d;for(splice @l,0,10){print"$_ $d{$_}\n"}