from heapq import heapify, heappop, heappush, heappushpop

T = int(input())
for i in range(T):
    N = int(input())
    sum = 0
    l1 = []
    l2 = []
    for j in range(N):
        a = list(map(int, input().split()))
        if a[1] > a[2]:
            l1.append((-a[0], abs(a[1] - a[2])))
            sum += a[2]
        else:
            if a[0] != N:
                l2.append((a[0], abs(a[2] - a[1])))
            sum += a[1]
    sl1 = sorted(l1)
    sl2 = sorted(l2)
    h1 = []
    h2 = []
    poped = 0
    for j in range(1, N + 1):
        while len(sl1) > 0 and j == -sl1[-1][0]:
            l = sl1.pop()
            heappush(h1, -l[1])
        while poped <= j and len(h1) > 0:
            sum += -heappop(h1)
            poped += 1
    poped = 0
    for j in reversed(range(1, N)):
        while len(sl2) > 0 and j == sl2[-1][0]:
            l = sl2.pop()
            heappush(h2, -l[1])
        while poped <= N - j and len(h2) > 0:
            sum += -heappop(h2)
            poped += 1

    print(sum)
