T = int(input())

import heapq  # heapqライブラリのimport

for case in range(T):
    N = int(input())
    KLR = []
    for i in range(N):
        KLR.append(list(map(int,input().split())))
    heapq.heapify(KLR)
    Ls = [[0,0,0]]
    heapq.heapify(Ls)
    Rs = []
    now = 0
    A,AL,AR = heapq.heappop(KLR)
    while now<=N-1:
        if now<A-1:
            heapq.heappush(Ls, [0,0,0])
            now+=1
        elif now==A-1:
            test = heapq.heappop(Ls)
            if AL-AR>=test[1]-test[2]:
                heapq.heappush(Ls,[A,AL,AR])
            else:
                heapq.heappush(Ls,test)
                if A==N:
                    Rs.append(AL)
                else:
                    Rs.append(AR)
            if len(KLR)==0:
                break
            A,AL,AR = heapq.heappop(KLR)
    out = 0
    for i in range(len(Ls)):
        out += Ls[i][1]
    for i in range(len(Rs)):
        out += Rs[i]
    print(out)