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


def func(left):
    ll = len(left)
    left.sort(key=lambda x: (x[0], -x[1]))
    val = 0
    idx = 0
    S = 0
    while idx < ll:
        k = left[idx][0]
        while idx < ll and left[idx][0] == k:
            _, v = left[idx]
            if S < k:
                val += v
                S += 1
            idx += 1
    return val


def main():
    T = int(input())
    for _ in range(T):
        n = int(input())
        ans = 0
        left = []
        right = []
        for _ in range(n):
            k, l, r = map(int, input().split())
            if l > r:
                ans += r
                left.append((k, l - r))
            else:
                ans += l
                right.append((n - k, r - l))
        ans += func(left)
        ans += func(right)
        print(ans)


if __name__ == "__main__":
    main()
