T=int(input())

for t in range(T):
    N=int(input())
    KLR = [tuple(map(int,input().split())) for _ in range(N)]
    DKLR2 = [(R-L, K, i) for i,(K,L,R) in enumerate(KLR) if R-L>0]
    DKLR = [(L-R, K, i) for i,(K,L,R) in enumerate(KLR) if R-L<=0]
    DKLR = sorted(DKLR, reverse=True)
    DKLR2 = sorted(DKLR2, reverse=True)
    res = 0
    left = 0
    right = 0
    for dv,k,i in DKLR:
        if k!=0:
            left += 1
            res += KLR[i][1]
        else:
            res += KLR[i][2]
    for dv,k,i in DKLR2:
        if k!=N:
            right += 1
            res += KLR[i][2]
        else:
            res += KLR[i][1]
    print(res)