import sys
input = sys.stdin.readline

t = int(input()[:-1])

for loop in range(t):
    n = int(input()[:-1])
    lg = []
    rg = []
    for i in range(n):
        k,l,r = map(int,input()[:-1].split())
        if l > r:
            lg.append([k,l,r])
        else:
            rg.append([n-k+1,l,r])
    lg.sort(key=lambda x:(x[0],-x[1]))
    rg.sort(key=lambda x:(x[0],-x[2]))
    fill = 0
    ans = 0
    for l in lg:
        if l[0] > fill:
            fill += 1
            ans += l[1]
        else:
            fill += 1
            ans += l[2]
    fill = 0
    for r in rg:
        if r[0] > fill:
            fill += 1
            ans += r[2]
        else:
            fill += 1
            ans += r[1]
    # print(lg)
    # print(rg)
    print(ans)