import heapq
for _ in range(int(input())):
    N=int(input())
    ListL=[]
    ListR=[]
    ans=0
    for _ in range(N):
        k,l,r=map(int,input().split())
        ans+=min(l,r)
        if l>=r:
            ListL.append([k-1,abs(l-r)])
        else:
            ListR.append([N-k,abs(l-r)])
    S=[]
    for i in range(N):
        for j in range(len(ListL)):
            if ListL[j][0]==i:
                heapq.heappush(S,ListL[j][1])
        while len(S)>i+1:
            heapq.heappop(S) 
    ans+=sum(S)
    S=[]
    for i in range(1,N):
        for j in range(len(ListR)):
            if ListR[j][0]==i:
                heapq.heappush(S,ListR[j][1])
        while len(S)>i+1:
            heapq.heappop(S) 
    ans+=sum(S)

    print(ans)