t=int(input())
import heapq
for _ in range(t):
    n=int(input())
    L=[];R=[]
    ans=0
    for _ in range(n):
        ki,li,ri = map(int, input().split())
        dif=li-ri
        if dif>=0:
            L.append((ki-1,dif))
            ans+=ri
        else:
            R.append((n-ki-1,-dif))
            ans+=li
    L.sort();R.sort()
    hL=[]
    hR=[]
    cnt=0
    for i in range(n-1,-1,-1):
        if L:
            while L and L[-1][0]==i:
                heapq.heappush(hL,-L[-1][1])
                L.pop()
            while hL and cnt<=n-i:
                ad=heapq.heappop(hL)
                ans-=ad
                cnt+=1
        else:
            while hL and cnt<=n-i:
                ad=heapq.heappop(hL)
                ans-=ad
                cnt+=1
    cnt=0
    for i in range(n-1,-1,-1):
        if R:
            while R and R[-1][0]==i:
                heapq.heappush(hR,-R[-1][1])
                R.pop()
            while hR and cnt<=n-i:
                ad=heapq.heappop(hR)
                ans-=ad
                cnt+=1
        else:
            while hR and cnt<=n-i:
                ad=heapq.heappop(hR)
                ans-=ad
                cnt+=1
    print(ans)        
    
    