t = int(input())
for i in range(t):
    n = int(input())
    ans = 0
    cl,cr = 0,0
    lis = []
    for i in range(n):
        k,l,r = map(int,input().split())
        a = abs(l-r)
        s = (l>r)
        lis.append([k,l,r,a,s])
    lis.sort(key=lambda x: x[3], reverse=True)
    for i in lis:
        if i[4] == 1:
            if cl<i[0]:
                ans += i[1]
            else:
                ans += i[2]
        else:
            if n-cr > i[0]:
                ans += i[2]
            else:
                ans += i[1]
    print(ans)

