class Node:
    def __init__(self, i, p, c):
        self.i = i
        if i != 0:
            self.p = p
        else:
            self.p = None
        self.c = [c]


class CamelSort:
    def __init__(self):
        self.all_camels = []
        self.g = []

        self.n = int(input())
        self.k, self.l, self.r = [], [], []

        self.lefts = []
        self.rights = []

        for i in range(self.n):
            _k, _l, _r = map(int, input().split())
            self.l.append(_l)
            self.r.append(_r)
            if _l > _r:
                self.k.append(_k - 1)
                self.lefts.append((i, _r, _l - _r))
            else:
                self.k.append(self.n - (_k) - 1)
                self.rights.append((i, _l, _r - _l))
        self.lefts = sorted(self.lefts, key=lambda r: r[1], reverse=True)
        self.rights = sorted(self.rights, key=lambda r: r[1], reverse=True)

        self.ans = self.get_ans(self.lefts) + self.get_ans(self.rights)

    def insert_camel(self, _k):
        if _k == -1:
            return False
        i = _k
        while i is not None:
            # print(i, self.all_camels, self.n)
            # print(self.g)
            if self.all_camels[i] == 0:
                self.all_camels[i] = 1
                p = self.g[i].p
                for c in self.g[i].c:
                    if c is None: break
                    self.g[c].p = p
                return True
            i = self.g[i].p
        return False

    def get_ans(self, camels):
        res = []
        self.all_camels = [0 for _ in range(self.n)]
        self.g = [Node(i, i - 1, i + 1) if i != self.n - 1 else Node(i, i - 1, None)
                  for i in range(self.n)]
        for i, base, diff in camels:
            if self.insert_camel(self.k[i]):
                res.append(base + diff)
            else:
                res.append(base)
        return sum(res)


def main():
    t = int(input())
    for _ in range(t):
        print(CamelSort().ans)


main()
