t = int(input())
for i in range(t):
    n = int(input())
    a1 = []
    a2 = []
    ans = 0
    flag = [False] * n
    for j in range(n):
        k, l, r = map(int, input().split())
        if l > r:
            a1.append([k, l, r])
        elif l < r:
            a2.append([k, r, l])
        else:
            ans += l
    a1.sort(key = lambda x: x[1] - x[2], reverse = True)
    a2.sort(key = lambda x: x[1] - x[2], reverse = True)
    for j in range(len(a1)):
        num = a1[j][0] - 1
        while num >= 0:
            if flag[num] == False:
                flag[num] = True
                ans += a1[j][1]
                break
            else:
                num -= 1
        else:
            ans += a1[j][2]
    for j in range(len(a2)):
        num = a2[j][0] - 1
        while num <= n - 2:
            if flag[num] == False:
                flag[num] = True
                ans += a2[j][1]
                break
            else:
                num += 1
        else:
            ans += a2[j][2]
    print(ans)