def solve():  
    ans = 0   
    N = int(input())
    KLR = []  
    for _ in range(N):
        KLR.append(list(map(int, input().split())))
    KLR.sort(key=lambda klr: -abs(klr[1] - klr[2]))
    train = [None] * N
    for klr in KLR:
        if klr[1] > klr[2]:
            pos = klr[0] - 1
            dir = -1
        elif klr[1] < klr[2]:
            pos = klr[0]
            dir = 1
        else: 
            pos = 0
            dir = 1
        if pos < 0 or pos >= N:
            ans += min(klr[1], klr[2])
            continue
        ok = False
        if train[pos] == None:
            ok = True
        else: 
            dir_ = train[pos]
            pos_ = pos
            while pos_ >= 0 and pos_ < N:
                if train[pos_] == None:
                    train[pos_] = dir_
                    train[pos] = None
                    ok = True
                    break
                pos_ += dir_
        if ok:
            train[pos] = dir
            ans += max(klr[1], klr[2])
        else: 
            ans += min(klr[1], klr[2])
    print(ans)
              
T = int(input())
              
for p in range(T):
    solve()   
              