t=int(input())
for _ in range(t):
    a=[]
    for _ in range(int(input())):
        a.append([int(x) for x in input().split()])
    fronta = [t for t in a if t[1] >= t[2]]
    backa = [t for t in a if t[1] < t[2]]
    fronta = sorted(fronta, key=lambda x: (x[0], x[1]))
    backa = sorted(backa, key=lambda x: (x[0], x[2]))
    s = 0
    i = 1
    for c in fronta+backa:
        if i <= c[0]:
            s += c[1]
        else:
            s += c[2]
        i += 1
    print(s)
