from heapq import heappush, heappop
t = int(input())
for _ in range(t):
    n = int(input())
    lis = [[] for _ in range(n+1)]
    s = 0
    for _ in range(n):
        k, l, r = map(int, input().split())
        s += l
        lis[k].append((l, r))
    que = []
    for i in range(n):
        for l, r in lis[i]:
            if r - l > 0:
                heappush(que, l-r)
        if len(que) > 0:
            x = heappop(que)
            s -= x

    print(s)