t = int(input())
while t:
    t-=1
    n = int(input())
    klr = [list(map(int, input().split())) for i in range(n)]
    klr.sort(key=lambda x:(x[0],-x[1]))
    ans = 0
    l = 0
    r = 0
    inc = 0
    for i in range(n):
        if(i>0):
            inc += klr[i][0]-klr[i-1][0]
        if (klr[i][1]>=klr[i][2] and l<klr[i][0]) or r>=n-klr[i][0]:
            if klr[i][1]<klr[i][2] and max(r-inc,0)<n-klr[i][0]:
                ans += klr[i][2]
                r += 1
                dec = r - (n-klr[i][0])
                inc -= dec
            else:
                ans += klr[i][1]
                l += 1
        else:
            ans += klr[i][2]
            r += 1
    print(ans)
    