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