import heapq

def main2(l, n):
    ans = 0
    length = len(l[0])
    lis = heapq.heapify(l[0])
    while length > 1:
        length -= 1
        ans += heapq.heappop(lis)
    for i in range(1, n):
        length += len(l[i])
        for j in l[i]:
            heapq.heappush(lis, j)
        while length > i + 1:
            length -= 1
            ans += heapq.heappop(lis)
    return ans

def main():
    from sys import stdin
    def input():
        return stdin.readline().strip()

    t = int(input())

    answer = []
    for _ in range(t):
        n = int(input())
        ans = 0
        l = [[] for _ in range(n)]
        r = [[] for _ in range(n)]
        for _ in range(n):
            i, j, k = map(int, input().split())
            if j > k:
                ans += j
                l[i-1].append(j-k)
            if j < k:
                ans += k
                r[n-i].append(k-j)
            ans -= main2(l, n) + main2(r, n)
            answer.append(ans)

    for i in answer:
        print(i)

main()