import bisect
T=int(input())
class Segtree(object):
    def __init__(self, init_val, N):
        self.func = self.segfunc_max
        self.ide_ele = 0 # max

        self.num = 2**(N-1).bit_length()
        self.seg = [self.ide_ele]*2*self.num
        self.init_val = init_val

        n = len(self.init_val)
        self.seg[self.num-1:n+self.num-1] = self.init_val[:n]
        #built
        for i in range(self.num-2,-1,-1) :
            self.seg[i] = self.func(self.seg[2*i+1], self.seg[2*i+2])

    def update(self, k, x):
        k += self.num-1
        self.seg[k] = x
        while k:
            k = (k-1)//2
            self.seg[k] = self.func(self.seg[k*2+1], self.seg[k*2+2])

    def query(self, p, q):
        if q<=p:
            return -1
        p += self.num-1
        q += self.num-2
        res = self.ide_ele
        while q-p>1:
            if self.seg[p]==0 and self.seg[q]==0:
                return -1
            elif self.seg[q]==0:
                q = (q-1)//2
                res = self.seg[p]
                continue
            else:
                res = self.seg[q]
                p = p//2
                continue
        if p == q:
            # res = self.func(res,self.seg[p])
            res = -1
        return res
    def query2(self, p, q):
        if q<=p:
            return self.ide_ele
        p += self.num-1
        q += self.num-2
        res = self.ide_ele
        while q-p>1:
            if p&1 == 0:
                res = self.func(res,self.seg[p])
            if q&1 == 1:
                res = self.func(res,self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.func(res,self.seg[p])
        else:
            res = self.func(self.func(res,self.seg[p]),self.seg[q])
        return res
    def segfunc_max(self, a, b):
        if a >= b:
            return a
        return b

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
    L1 = len(DKLR)
    L2 = len(DKLR2)
    left = list(range(1,L1+1))
    right = list(range(1,L2+1))
    seg1 = Segtree(left,L1)
    seg2 = Segtree(right,L2)
    # print(DKLR)
    # print(DKLR2)
    for dv,k,i in DKLR:
        # print(seg1.seg)
        if k <= L1:
            idx = seg1.query2(0,k)-1
            # print(idx)
            if idx!=-1:
                seg1.update(idx, 0)
                res += KLR[i][1]
            else:
                res += KLR[i][2]
        else:
            res += KLR[i][1]
    for dv,k,i in DKLR2:
        # print(seg2.seg)
        if k >= N-L2:
            idx = seg2.query2(0,N-k)-1
            # print(idx)
            if idx!=-1:
                seg2.update(idx, 0)
                res += KLR[i][2]
            else:
                res += KLR[i][1]
        else:
            res += KLR[i][2]
    # for dv,k,i in DKLR2:
    #     idx = seg2.query2(0,k)
    #     if idx!=-1:
    #         seg2.update(idx, 0)
    #         res += KLR[i][2]
    #     else:
    #         res += KLR[i][1]
    print(res)

Measured memory_bytes: 134968