import sys
def input(): return sys.stdin.readline().strip()


def main():
    T = int(input())
    for _ in range(T):
        N = int(input())
        # 左右独立に解いて問題ない
        left = []
        right = []
        for i in range(N):
            k, l, r = map(int, input().split())
            if l > r: left.append((k, l, r))
            else: right.append((k, l, r))
            left.sort(key=lambda x: (x[0], -x[1]))
            right.sort(key=lambda x: (-x[0], -x[2]))
        ll = len(left)
        rl = len(right)
        #print("left={}".format(left))
        #print("right={}".format(right))

        ans = 0
        # 左側
        idx = 0
        S = 0
        while idx < ll:
            k = left[idx][0]
            while idx < ll and left[idx][0] == k:
                _, l, r = left[idx]
                if S < k:
                    ans += l
                    S += 1
                else:
                    ans += r
                idx += 1
        #print("left_val={}".format(ans))
        # 右側
        idx = 0
        S = N + 1
        while idx < rl:
            k = right[idx][0]
            while idx < rl and right[idx][0] == k:
                _, l, r = right[idx]
                if S > k + 1:
                    ans += r
                    S -= 1
                else:
                    ans += l
                idx += 1

        print(ans)


if __name__ == "__main__":
    main()
