#!python3

import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())

def resolve():
    it = map(int, sys.stdin.read().split())

    T = next(it)
    ans = []
    for i in range(T):
        N = next(it)

        val = 0; a1 = []; a2 = []
        for i, v1, v2 in ((next(it), next(it), next(it)) for i in range(N)):
            diff = v1 - v2
            a, v, diff = (a1, v2, diff) if diff >= 0 else (a2, v1, -diff)
            a.append((diff, i))
            val += v
        a1.sort(reverse=True); a2.sort(reverse=True)

        ls = len(a1)
        vis = [0]*N
        for diff, i in a1:
            for j in reversed(range(min(i,ls))):
                if vis[j]: continue
                vis[j] = 1
                val += diff
                break

        for diff, i in a2:
            for j in range(max(i, ls), N):
                if vis[j]: continue
                vis[j] = 1
                val += diff
                break

        ans.append(val)

    print(*ans, sep="\n")



if __name__ == "__main__":
    resolve()
