import sys

from heapq import heappop, heappush



ans_l = []

T = int(input())

for t in range(T):

    ans = 0

    N = int(input())

    left = []

    right = []

    neutral = []

    for i in range(N):

        K, L, R = map(int, sys.stdin.readline().split())

        if L > R:

            heappush(left, (K, L - R, L, R))

        elif L < R:

            heappush(right, (-K, R - L, L, R))

        else:

            ans += L

    # left

    pool = []

    pos = 1

    while left:

        while left and left[0][0] == pos:

            k, d, l, r = heappop(left)

            heappush(pool, (d, l, r))

        while len(pool) > pos:

            d, l, r = heappop(pool)

            ans += r

        pos += 1

    while pool:

        d, l, r = heappop(pool)

        ans += l

            

    # right

    pool = []

    pos = N

    while right:

        while right and right[0][0] == -pos:

            k, d, l, r = heappop(right)

            heappush(pool, (d, l, r))

        while len(pool) > N - pos:

            d, l, r = heappop(pool)

            ans += l

        pos -= 1

    while pool:

        d, l, r = heappop(pool)

        ans += r



    ans_l.append(ans)





print(*ans_l, sep='\n')

import sys

from heapq import heappop, heappush



ans_l = []

T = int(input())

for t in range(T):

    N = int(input())

    left, right = [], []

    ans = 0

    for i in range(N):

        K, L, R = map(int, sys.stdin.readline().split())

        if L > R:

            ans += L

            left.append((K, L - R))

        elif L < R:

            ans += R

            right.append((K, R - L))

        else:

            ans += L

    # left

    pool = []

    pos = 1

    left.sort()

    cur = 0

    while cur < len(left):

        while cur < len(left) and left[cur][0] == pos:

            k, d = left[cur]

            heappush(pool, d)

            cur += 1

        while len(pool) > pos:

            d = heappop(pool)

            ans -= d

        pos += 1

            

    # right

    pool = []

    pos = N

    right.sort(reverse=True)

    cur = 0

    while cur < len(right):

        while cur < len(right) and right[cur][0] == pos:

            k, d = right[cur]

            heappush(pool, d)

            cur += 1

        while len(pool) > N - pos:

            d = heappop(pool)

            ans -= d

        pos -= 1



    ans_l.append(ans)





print(*ans_l, sep='\n')

import sys
from heapq import heappop, heappush

ans_l = []
T = int(input())
for t in range(T):
    ans = 0
    N = int(input())
    left = []
    right = []
    neutral = []
    for i in range(N):
        K, L, R = map(int, sys.stdin.readline().split())
        if L > R:
            heappush(left, (K, L - R, L, R))
        elif L < R:
            heappush(right, (-K, R - L, L, R))
        else:
            ans += L
    # left
    pool = []
    pos = 1
    while left:
        while left and left[0][0] == pos:
            k, d, l, r = heappop(left)
            heappush(pool, (d, l, r))
        while len(pool) > pos:
            d, l, r = heappop(pool)
            ans += r
        pos += 1
    while pool:
        d, l, r = heappop(pool)
        ans += l
    # right
    pool = []
    pos = N
    while right:
        while right and right[0][0] == -pos:
            k, d, l, r = heappop(right)
            heappush(pool, (d, l, r))
        while len(pool) > N - pos:
            d, l, r = heappop(pool)
            ans += l
        pos -= 1
    while pool:
        d, l, r = heappop(pool)
        ans += r
    ans_l.append(ans)
print(*ans_l, sep="\n")

import sys
from heapq import heappop, heappush

ans_l = []
T = int(input())
for t in range(T):
    N = int(input())
    left, right = [], []
    ans = 0
    for i in range(N):
        K, L, R = map(int, sys.stdin.readline().split())
        if L > R:
            ans += L
            left.append((K, L - R))
        elif L < R:
            ans += R
            right.append((K, R - L))
        else:
            ans += L
    # left
    pool = []
    pos = 1
    left.sort()
    cur = 0
    while cur < len(left):
        while cur < len(left) and left[cur][0] == pos:
            k, d = left[cur]
            heappush(pool, d)
            cur += 1
        while len(pool) > pos:
            d = heappop(pool)
            ans -= d
        pos += 1
    # right
    pool = []
    pos = N
    right.sort(reverse=True)
    cur = 0
    while cur < len(right):
        while cur < len(right) and right[cur][0] == pos:
            k, d = right[cur]
            heappush(pool, d)
            cur += 1
        while len(pool) > N - pos:
            d = heappop(pool)
            ans -= d
        pos -= 1
    ans_l.append(ans)
print(*ans_l, sep="\n")

+    N = int(input()) +    left, right = [], [] -    N = int(input()) -    left = [] -    right = [] -    neutral = [] -            heappush(left, (K, L - R, L, R)) +            ans += L +            left.append((K, L - R)) -            heappush(right, (-K, R - L, L, R)) +            ans += R +            right.append((K, R - L)) -    while left: -        while left and left[0][0] == pos: -            k, d, l, r = heappop(left) -            heappush(pool, (d, l, r)) +    left.sort() +    cur = 0 +    while cur < len(left): +        while cur < len(left) and left[cur][0] == pos: +            k, d = left[cur] +            heappush(pool, d) +            cur += 1 -            d, l, r = heappop(pool) -            ans += r +            d = heappop(pool) +            ans -= d -    while pool: -        d, l, r = heappop(pool) -        ans += l -    while right: -        while right and right[0][0] == -pos: -            k, d, l, r = heappop(right) -            heappush(pool, (d, l, r)) +    right.sort(reverse=True) +    cur = 0 +    while cur < len(right): +        while cur < len(right) and right[cur][0] == pos: +            k, d = right[cur] +            heappush(pool, d) +            cur += 1 -            d, l, r = heappop(pool) -            ans += l +            d = heappop(pool) +            ans -= d -    while pool: -        d, l, r = heappop(pool) -        ans += r