
class BinaryIndexedTree:
    def __init__(self, size):
        self.data = [0] * (size+1)
        self.msb = 1 << (size.bit_length()-1)
    
    def _add(self, i, w):
        i += 1
        while i < len(self.data):
            self.data[i] += w
            i += i & -i
    
    def _get_sum(self, i):
        res = 0
        while i > 0:
            res += self.data[i]
            i -= i & -i
        return res

    def __getitem__(self, i):
        """
        [0,i)
        """
        if isinstance(i, slice):
            if i.start is None:
                return self._get_sum(i.stop)
            else:
                return self._get_sum(i.stop) - self._get_sum(i.start)
        else:
            return 0 # fake value
    
    __setitem__ = _add
    
    def bisect_left(self, v):
        """
        return smallest i s.t v <= sum[:i]
        """
        i = 0
        k = self.msb
        while k > 0:
            i += k
            if i < len(self.data) and self.data[i] < v:
                v -= self.data[i]
            else:
                i -= k
            k >>= 1
        return i
    
    def bisect_right(self, v):
        """
        return smallest i s.t v < sum[:i]
        """
        i = 0
        k = self.msb
        while k > 0:
            i += k
            if i < len(self.data) and self.data[i] <= v:
                v -= self.data[i]
            else:
                i -= k
            k >>= 1
        return i
    
    bisect = bisect_right

def solve(KLR):
    min2 = lambda x,y: x if x < y else y

    N = len(KLR)
    offset = sum(min2(l,r) for k,l,r in KLR)
    KLR.sort()
    p = sorted((l-r,k,i,True) if l > r else (r-l,k,i,False) for i,(k,l,r) in enumerate(KLR))

    lbit = BinaryIndexedTree(N)
    rbit = BinaryIndexedTree(N)
    res = 0
    for v,k,i,is_l in p:
        if is_l:
            if lbit[:i]+1 <= k:
                lbit[i] += 1
                res += v
            else:
                rbit[i] += 1
        else:
            if rbit[i+1:N]+1 <= N-k:
                rbit[i] += 1
                res += v
            else:
                lbit[i] += 1
    return res+offset

if __name__ == '__main__':
    T = int(input())
    for t in range(T):
        N = int(input())
        KLR = [tuple(map(int,input().split())) for _ in range(N)]
        print(solve(KLR))
