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 = []
    score = 0
    for K, L, R in klr:
        if K != n:
            score += R
            if R < L:
                heapq.heappush(left, L-R)
        else:
            score += L

        if len(left) <= K:
            continue
        heapq.heappop(left)
    score += sum(left)
    print(score)
