# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
from heapq import heappush, heappop
import sys
def main(N, KLR):
    LL = [(-(l - r), k - 1, l, r) for k, l, r in KLR if l >= r]
    RR = [(-(r - l), N - k - 1, r, l) for k, l, r in KLR if l < r and k < N]
    ans = sum(l for k, l, r in KLR if l < r and k == N)
    for L in (LL, RR):
        M = len(L)
        L.sort(key=lambda x: x[1])
        q = []
        j = 0
        c = 0
        for i in range(N):
            while j < M and L[j][1] <= i:
                heappush(q, L[j])
                j += 1
            while q and c <= i:
                _, k, l, r = heappop(q)
                if c <= k:
                    ans += l
                else:
                    ans += r
                c += 1
    return ans

if __name__ == '__main__':
    input = sys.stdin.readline
    T = int(input())
    for i in range(T):
        N = int(input())
        KLR = [tuple(map(int, input().split())) for _ in range(N)]
        ans = main(N, KLR)
        print(ans)
