
    import heapq as hq

    T = int(input())
    for _ in range(T):
        N = int(input())
        camels = [list(map(int, input().split())) for _ in range(N)]
        base = 0
        camels_L = [[] for _ in range(N+1)]
        camels_R = [[] for _ in range(N+1)]
        for camel in camels:
            if camel[1] >= camel[2]:
                camels_L[camel[0]].append(camel[1] - camel[2])
                base += camel[2] 
            else:
                camels_R[N - camel[0]].append(camel[2] - camel[1])
                base += camel[1] 

        diff = 0


        heap_camels = []
        hq.heapify(heap_camels)
        for i in range(1, N+1):
            # 左からi番目までが限度のラクダをまとめて追加
            for camel in camels_L[i]:
                hq.heappush(heap_camels, camel)
            # 左からi番目までに入りきらない場合に低いものから除外
            while len(heap_camels) > i:
                hq.heappop(heap_camels)
        # heapqに残ったものがすべての条件を満たしうれしさが最大
        diff += sum(heap_camels)

        # Rラクダについても同様
        heap_camels = []
        hq.heapify(heap_camels)
        for i in range(1, N+1):
            for camel in camels_R[i]:
                hq.heappush(heap_camels, camel)
            while len(heap_camels) > i:
                hq.heappop(heap_camels)
        diff += sum(heap_camels)
        print(diff + base)
