t = int(input())
for _ in range(t):
    n = int(input())
    a = []
    b = []
    for _ in range(n):
        k,l,r = map(int, input().split())
        if l>r:
            a.append((k,l,r))
        else:
            b.append((k,l,r))
    a.sort(key=lambda x:x[0])
    b.sort(key=lambda x: x[0])

    ans = 0
    for i,(k,l,r) in enumerate(a+b):
        if i+1<=k:
            ans += l
        else:
            ans += r
    print(ans)

