from heapq import heappop, heappush, heapify

T = int(input())

for _ in range(T):
    N = int(input())
    camel = [tuple(map(int, input().split())) for i in range(N)]
    dif = [(camel[i][1] - camel[i][2], camel[i][0] - 1, i) for i in range(N)]

    pos = []
    neg = []

    for i in range(N):
        if dif[i][0] >= 0:
            pos.append((-dif[i][0], dif[i][1], dif[i][2]))
        else:
            neg.append(dif[i])

    p = len(pos)
    n = len(neg)

    pos.sort(key=lambda x: x[1], reverse=True)
    neg.sort(key=lambda x: x[1])

    heappos = []
    heapneg = []

    arr = [None for i in range(N)]

    j = 0
    for i in range(p)[::-1]:
        f = False
        while j != p and pos[j][1] >= i:
            heappush(heappos, pos[j])
            j += 1
            f = True
        #if not f and j != p:
        #    heappush(heappos, pos[j])
        #    j += 1
        d, k, id = heappop(heappos)
        arr[i] = id

    j = 0
    for i in range(n):
        f = False
        while j != n and neg[j][1] <= p + i:
            heappush(heapneg, neg[j])
            j += 1
            f = True
        #if not f and j != n:
        #    heappush(heapneg, neg[j])
        #    j += 1
        d, k, id = heappop(heapneg)
        arr[p + i] = id
    res = 0

    for i in range(N):
        k, l, r = camel[arr[i]]
        if k >= i + 1:
            res += l
        else:
            res += r

    print(res)