from heapq import heappush, heappop
it = lambda: list(map(int, input().strip().split()))


def greedy(A):
    ans = 0
    A.sort(key=lambda x:x[1] - x[2])
    pq = []
    while A:
        k, a, b = A.pop()
        if k > 0 and (k > len(pq) or -pq[0][0] >= len(pq) + 1):
            heappush(pq, [-k, a, b])
            ans += a
        else:
            ans += b
    return ans


def solve():
    N = int(input())

    prefer_late = []
    prefer_early = []
    does_not_matter = []
    for _ in range(N):
        k, a, b = it()
        if a == b:
            does_not_matter.append([k, a, b])
        elif a > b:
            prefer_early.append([k, a, b])
        else:
            prefer_late.append([N - k, b, a])
    
    ans = 0
    ans += greedy(prefer_early)
    ans += greedy(prefer_late)
    for k, a, b in does_not_matter:
        ans += a
    print(ans)


T = int(input())
for _ in range(T):
    solve()