import heapq

def main():
    T = int(input())
    for _ in range(T):
        N = int(input())
        KLR = [tuple(map(int, input().split())) for _ in range(N)]
        KLR.sort()
        H = []
        s = 0
        for k, l, r in KLR:
            s += r
            heapq.heappush(H, (l - r, k))
            while len(H) > k or (H and H[0][1]<= len(H)):
                heapq.heappop(H)
        print(s + sum(l for l, k in H))
main()
