import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from heapq import heappop, heappush
def resolve():
    for _ in range(int(input())):
        n = int(input())
        heaps = [[] for _ in range(n)]
        ans = 0
        for i in range(n):
            k, l, r = map(int, input().split())
            ans += l
            k -= 1
            heappush(heaps[k], -(r - l))
        candidate = []
        for i in range(n):
            if candidate:
                ans -= min(heappop(candidate), 0)
            while heaps[i]:
                heappush(candidate, heaps[i].pop())

        print(ans)
resolve()