import heapq
t = int(input())
for _ in range(t):
    n = int(input())
    klr = [tuple(map(int, input().split())) for _ in range(n)]
    klr.sort()
    left = []
    right = []
    score = 0
    for K,L,R in klr:
        if L<R:
            right.append((K,L,R))
        elif R<L:
            left.append((K,L,R))
        else:
            score+=L

    right.sort(reverse=True)
    left.sort()

    use_left = []
    for K,L,R in left:
        if K!=n:
            score+=R
            heapq.heappush(use_left,L-R)
        else:
            score+=L
            heapq.heappush(use_left,0)

        if K<len(use_left):
            heapq.heappop(use_left)
        

    use_right = []
    for K,L,R in right:
        if K!=n:
            score+=L
            heapq.heappush(use_right,R-L)
        else:
            score+=L
            heapq.heappush(use_right,0)


        if (n-K+1)<len(use_right):
            heapq.heappop(use_right)

    score+=sum(use_left)+sum(use_right)
    print(score)
