T=int(input())
for case in range(T):
    N=int(input())
    Left=[]
    Right=[]
    Middle=[]
    for i in range(N):
        k,l,r=map(int,input().split())
        if k==N or l==r:
            Middle.append((k,l,r))
        elif l<r:
            Right.append((k,l,r))
        else:
            Left.append((k,l,r))
    Left.sort(key=lambda x:(x[0],x[1]-x[2]))
    Right.sort(key=lambda x:(-x[0],x[2]-x[1]))
    ans=0
    i=1
    for x in Left:
        k,l,r=x
        if i<=k:
            ans+=l
            i+=1
        else:
            ans+=r
    i=N
    for x in Right:
        k,l,r=x
        if i>k:
            ans+=r
            i-=1
        else:
            ans+=l
    for x in Middle:
        k,l,r=x
        ans+=l
    print(ans)
            