import heapq
T=int(input())
for t in range(T):
    N=int(input())
    ListL=[]
    ListR=[]
    ans=0
    for n in range(N):
        k,l,r=map(int,input().split())
        ans+=min(l,r)
        if min(l,r)==r:
            ListL.append([k-1,abs(l-r)])
        else:
            ListR.append([k-1,abs(l-r)])
    ListL=sorted(ListL,key=lambda x: x[0])
    ListR=sorted(ListR,key=lambda x: x[0])
    S=[]
    for j in range(N):
        for s in range(len(S),len(ListL)):
            if ListL[s][0]==j:
                heapq.heappush(S,ListL[s][1])
            else:
                break
        while len(S)>j+1:
            heapq.heappop(S) 
    ans+=sum(S)

    S=[]
    for j in range(N):
        for s in range(len(S),len(ListR)):
            if ListR[s][0]==j:
                heapq.heappush(S,ListR[s][1])
            else:
                break
        while len(S)>j+1:
            heapq.heappop(S) 
    ans+=sum(S)

    print(ans)