import sys

from heapq import heappush, heappop



readline = sys.stdin.readline





def solve():

    N = int(readline())



    left = []

    right = []

    ans = 0

    for _ in range(N):

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

        if L > R:

            ans += R

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

        else:

            ans += L

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



    for vec in (left, right):

        vec.sort()

        hq = []

        for idx in range(len(vec) - 1, -1, -1):

            while vec and idx <= vec[-1][0]:

                heappush(hq, vec.pop()[1])

            if hq:

                ans -= heappop(hq)



    return ans





def main():

    T = int(readline())



    ans = [0] * T

    for i in range(T):

        ans[i] = solve()



    print(('\n'.join(map(str, ans))))

    return





if __name__ == '__main__':

    main()

import sys

from heapq import heappush, heappop



readline = sys.stdin.readline





def solve():

    N = int(readline())



    left = [[] for _ in range(N)]

    right = [[] for _ in range(N)]

    ans = 0

    for _ in range(N):

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

        if L > R:

            ans += R

            left[K - 1].append(R - L)

        else:

            ans += L

            if 0 <= N - K - 1:

                right[N - K - 1].append(L - R)



    for vec in (left, right):

        hq = []

        n = len(vec)

        for idx in range(N - 1, -1, -1):

            for p in vec[idx]:

                heappush(hq, p)

            if idx < n and hq:

                ans -= heappop(hq)



    return ans





def main():

    T = int(readline())



    ans = [0] * T

    for i in range(T):

        ans[i] = solve()



    print(('\n'.join(map(str, ans))))

    return





if __name__ == '__main__':

    main()

import sys
from heapq import heappush, heappop

readline = sys.stdin.readline


def solve():
    N = int(readline())
    left = []
    right = []
    ans = 0
    for _ in range(N):
        K, L, R = list(map(int, readline().split()))
        if L > R:
            ans += R
            left.append((K - 1, R - L))
        else:
            ans += L
            right.append((N - K - 1, L - R))
    for vec in (left, right):
        vec.sort()
        hq = []
        for idx in range(len(vec) - 1, -1, -1):
            while vec and idx <= vec[-1][0]:
                heappush(hq, vec.pop()[1])
            if hq:
                ans -= heappop(hq)
    return ans


def main():
    T = int(readline())
    ans = [0] * T
    for i in range(T):
        ans[i] = solve()
    print(("\n".join(map(str, ans))))
    return


if __name__ == "__main__":
    main()

import sys
from heapq import heappush, heappop

readline = sys.stdin.readline


def solve():
    N = int(readline())
    left = [[] for _ in range(N)]
    right = [[] for _ in range(N)]
    ans = 0
    for _ in range(N):
        K, L, R = list(map(int, readline().split()))
        if L > R:
            ans += R
            left[K - 1].append(R - L)
        else:
            ans += L
            if 0 <= N - K - 1:
                right[N - K - 1].append(L - R)
    for vec in (left, right):
        hq = []
        n = len(vec)
        for idx in range(N - 1, -1, -1):
            for p in vec[idx]:
                heappush(hq, p)
            if idx < n and hq:
                ans -= heappop(hq)
    return ans


def main():
    T = int(readline())
    ans = [0] * T
    for i in range(T):
        ans[i] = solve()
    print(("\n".join(map(str, ans))))
    return


if __name__ == "__main__":
    main()

-    left = [] -    right = [] +    left = [[] for _ in range(N)] +    right = [[] for _ in range(N)] -            left.append((K - 1, R - L)) +            left[K - 1].append(R - L) -            right.append((N - K - 1, L - R)) +            if 0 <= N - K - 1: +                right[N - K - 1].append(L - R) -        vec.sort() -        for idx in range(len(vec) - 1, -1, -1): -            while vec and idx <= vec[-1][0]: -                heappush(hq, vec.pop()[1]) -            if hq: +        n = len(vec) +        for idx in range(N - 1, -1, -1): +            for p in vec[idx]: +                heappush(hq, p) +            if idx < n and hq: