def calc(cammels):
    ans = 0
    n = len(cammels)
    cammels.sort()
    cammels_pos = 0
    for i in range(n):
        (k, score) = cammels[i]
        if cammels_pos + 1 <= k:
            ans += -score
            cammels_pos += 1
    return ans

def solve():
    n = int(input())
    base = 0
    lefts=[]
    rights=[]
    for i in range(n):
        k, l, r = map(int, input().split())
        base += min(l, r)
        if l > r:
            lefts.append((k, -(l - r)))
        if r > l:
            if k != n:
                rights.append((n-k, -(r - l)))

    print(base + calc(lefts) + calc(rights))
            


def main():
    T = int(input())
    for _ in range(T):
        solve()

main()