while 0<n=gets.to_i
puts (1..n).map{n,*d=gets.split.map &:to_i
t=0
while m=d[t..-2].index(10)
d[t+=m+1,0]=[0]
end
d[23]=s=0
d.each_slice(2).each_cons(3){|(a,b),(c,d),(e,_)|s+=a+b+(a+b>9?c:0)+(a>9?c>9?e:d :0)}
[-s,n]}.sort.map{|i,j|[j,-i]*" "}
end