import sys
import heapq
input = sys.stdin.readline

def main():
    t = int(input())
    for i in range(t):
        n = int(input())
        ans = 0
        judge_f = [[] for _ in range(n)]
        judge_b = [[] for _ in range(n)]
        for j in range(n):
            k, l, r = map(int, input().split())
            ans += min(l, r)
            if r < l:
                judge_b[k-1].append(l-r)
            elif l < r:
                if k != n:
                    judge_f[k].append(r-l)
        plus = [0]*n
        minus = [0]*n
        key = []
        heapq.heapify(key)
        for j in range(n):
            if judge_b[j]:
                for v in judge_b[j]:
                    heapq.heappush(key, -v)
            if key:
                minus[j] = -heapq.heappop(key)
        key = []
        heapq.heapify(key)
        for j in range(n-1, -1, -1):
            if judge_f[j]:
                for v in judge_f[j]:
                    heapq.heappush(key, -v)
            if key:
                plus[j] = -heapq.heappop(key)
        for i in range(1, n):
            plus[i] += plus[i-1]
            minus[n-i-1] += minus[n-i]
        
        count = 0
        for i in range(n):
            if count < plus[i]+minus[i]:
                count = plus[i]+minus[i]
        print(ans+count)

if __name__ == "__main__":
    main()