T = int(input())
Kli = [[] for _ in range(T)]
Lli = [[] for _ in range(T)]
Rli = [[] for _ in range(T)]
Nli = [[] for _ in range(T)]
for t in range(T):
    N = int(input())
    for n in range(N):
        K, L, R = map(int, input().split())
        Rli[t].append(R)
        Nli[t].append([K, L-R])
ansli = [[0 for _ in range(N+1)] for _ in range(T)]
for t in range(T):
    Nli[t].sort(key=lambda x: x[1], reverse = True)
    for i in range(len(Nli[t])):
        if Nli[t][i][1] < 0:
            for j in range(1,len(ansli[t])):
                if ansli[t][-j] == 0:
                    if len(ansli[t])-j > Nli[t][i][0]:
                        break
                    else:
                        ansli[t][-j] = Nli[t][i][1]
                        break
        elif ansli[t][Nli[t][i][0]] == 0:
            ansli[t][Nli[t][i][0]] = Nli[t][i][1]
        else:
            for k in range(1,Nli[t][i][0]):
                if ansli[t][Nli[t][i][0]-k] == 0:
                    ansli[t][Nli[t][i][0]-k] = Nli[t][i][1]
                    break
for t in range(T):
    print(sum(ansli[t])+sum(Rli[t]))