from heapq import heappop, heappush, heapify
from collections import deque

T = int(input())

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

    pos = []
    neg = []

    for i in range(N):
        k, l, r = camel[i]
        if l - r >= 0:
            pos.append((r - l, k, i))
        else:
            neg.append((l - r, N - k, i))

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

    heappos = []
    respos = [None for i in range(N)]

    j = 0
    for i in range(N)[::-1]:
        while j < p and pos[j][1] == i + 1:
            heappush(heappos, pos[j])
            j += 1
        if heappos:
            d, k, id = heappop(heappos)
            respos[i] = id
    for i in range(N)[::-1]:
        if respos[i] is None and heappos:
            d, k, id = heappop(heappos)
            respos[i] = id


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

    heapneg = []
    resneg = [None for i in range(N)]

    j = 0
    for i in range(N)[::-1]:
        while j < n and neg[j][1] == i + 1:
            heappush(heapneg, neg[j])
            j += 1
        if heapneg:
            d, k, id = heappop(heapneg)
            resneg[i] = id
    while j < n:
        heappush(heapneg, neg[j])
        j += 1
    for i in range(N)[::-1]:
        if resneg[i] is None and heapneg:
            d, k, id = heappop(heapneg)
            resneg[i] = id

    respos = [respos[i] for i in range(N) if respos[i] is not None]
    resneg = [resneg[i] for i in range(N) if resneg[i] is not None]
    res = respos + resneg[::-1]

    ans = 0

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

    print(ans)