import heapq  
def priority(klr):
    return abs(klr[1]-klr[2])
              
def process(KLR):
    ans = 0   
    q = []    
    for klr in KLR:
        pr = priority(klr)
        if klr[0] < len(q) and q[0][-1] < pr:
            tmp = heapq.heappop(q)
            ans += min(tmp[1], tmp[2])
        if klr[0] >= len(q):
            klr.append(pr)
            heapq.heappush(q, klr)
    for klr in q:
        ans += max(klr[1], klr[2])
    return ans
              
              
def solve():  
    N = int(input())
    KLR = []  
    for _ in range(N):
        klr = list(map(int, input().split()))
        klr[0] -= 1 # convert to 0-based index to avoid confusion                                                                                                                                           
        KLR.append(klr)
    ans = 0   
    KLR_L = []
    KLR_R = []
    for klr in KLR:
        if klr[1] > klr[2]:
            KLR_L.append(klr)
        elif klr[1] < klr[2]:
            klr[0] = N-1-klr[0]-1
            if klr[0] < 0:
                ans += min(klr[1], klr[2])
            else:
                KLR_R.append(klr)
        else: 
            ans += klr[1]
    KLR_L.sort(key=lambda klr: klr[0])
    KLR_R.sort(key=lambda klr: klr[0])
    ans += process(KLR_L)
    ans += process(KLR_R)
    print(ans)
              
T = int(input())
              
for _ in range(T):
    solve()   
              