from collections import deque

from heapq import heappush, heappushpop

import sys

input = sys.stdin.readline



def calc(camels):

    camels = deque(sorted(camels, key=lambda x: x[0]))

    N = len(camels)

    heap = []

    while camels and camels[0][0] == 0:

        camels.popleft()

    for i in range(1, N+1):

        while camels and camels[0][0] == i:

            _, x = camels.popleft()

            if len(heap) < i:

                heappush(heap, x)

            elif heap[0] < x:

                heappushpop(heap, x)

    for _, x in camels:

        if len(heap) < N:

            heappush(heap, x)

        elif heap[0] < x:

            heappushpop(heap, x)

    return sum(heap)



T = int(eval(input()))

for _ in range(T):

    N = int(eval(input()))

    s = 0

    ans = 0

    first = []

    second = []

    for i in range(N):

        K, L, R = list(map(int, input().split()))

        if L >= R:

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

            ans += R

        else:

            second.append((N - K, R - L))

            ans += L

    ans += calc(first) + calc(second)

    print(ans)
from heapq import heappush, heappop

import sys

input = sys.stdin.readline



def calc(camels, N):

    camels.sort(key=lambda x: x[0])

    heap = []

    for i, x in camels:

        heappush(heap, x)

        if len(heap) > i:

            heappop(heap)

    return sum(heap)



T = int(eval(input()))

for _ in range(T):

    N = int(eval(input()))

    s = 0

    ans = 0

    first = []

    second = []

    for i in range(N):

        K, L, R = list(map(int, input().split()))

        if L >= R:

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

            ans += R

        else:

            second.append((N - K, R - L))

            ans += L

    ans += calc(first, N) + calc(second, N)

    print(ans)
from collections import deque
from heapq import heappush, heappushpop
import sys

input = sys.stdin.readline


def calc(camels):
    camels = deque(sorted(camels, key=lambda x: x[0]))
    N = len(camels)
    heap = []
    while camels and camels[0][0] == 0:
        camels.popleft()
    for i in range(1, N + 1):
        while camels and camels[0][0] == i:
            _, x = camels.popleft()
            if len(heap) < i:
                heappush(heap, x)
            elif heap[0] < x:
                heappushpop(heap, x)
    for _, x in camels:
        if len(heap) < N:
            heappush(heap, x)
        elif heap[0] < x:
            heappushpop(heap, x)
    return sum(heap)


T = int(eval(input()))
for _ in range(T):
    N = int(eval(input()))
    s = 0
    ans = 0
    first = []
    second = []
    for i in range(N):
        K, L, R = list(map(int, input().split()))
        if L >= R:
            first.append((K, L - R))
            ans += R
        else:
            second.append((N - K, R - L))
            ans += L
    ans += calc(first) + calc(second)
    print(ans)

from heapq import heappush, heappop
import sys

input = sys.stdin.readline


def calc(camels, N):
    camels.sort(key=lambda x: x[0])
    heap = []
    for i, x in camels:
        heappush(heap, x)
        if len(heap) > i:
            heappop(heap)
    return sum(heap)


T = int(eval(input()))
for _ in range(T):
    N = int(eval(input()))
    s = 0
    ans = 0
    first = []
    second = []
    for i in range(N):
        K, L, R = list(map(int, input().split()))
        if L >= R:
            first.append((K, L - R))
            ans += R
        else:
            second.append((N - K, R - L))
            ans += L
    ans += calc(first, N) + calc(second, N)
    print(ans)

-from collections import deque -from heapq import heappush, heappushpop +from heapq import heappush, heappop -def calc(camels): -    camels = deque(sorted(camels, key=lambda x: x[0])) -    N = len(camels) +def calc(camels, N): +    camels.sort(key=lambda x: x[0]) -    while camels and camels[0][0] == 0: -        camels.popleft() -    for i in range(1, N + 1): -        while camels and camels[0][0] == i: -            _, x = camels.popleft() -            if len(heap) < i: -                heappush(heap, x) -            elif heap[0] < x: -                heappushpop(heap, x) -    for _, x in camels: -        if len(heap) < N: -            heappush(heap, x) -        elif heap[0] < x: -            heappushpop(heap, x) +    for i, x in camels: +        heappush(heap, x) +        if len(heap) > i: +            heappop(heap) -    ans += calc(first) + calc(second) +    ans += calc(first, N) + calc(second, N)