T = int(input())
for _ in range(T):
    N = int(input())
    KLR = [list(map(int, input().split())) for i in range(N)]
    ans = 0
    KLR.sort(key=lambda x: x[2])

    for i, klr in enumerate(KLR):
        K, L, R = klr
        if R >= L and N >= K:
            ans += R
            N -= 1
            KLR[i][2] = -1
    # print(ans, KLR)
    KLR = [(K, L, R) for K, L, R in KLR if R >= 0]
    KLR.sort(key=lambda x: x[0])
    l = 1
    for K, L, R in KLR:
        if K >= l:
            ans += L
            l += 1
        else:
            ans += R
            N -= 1
    print(ans)
