T = int(input())
for _ in range(T):
    N = int(input())
    favdic_l = {}
    favdic_r = {}
    total = 0
    for i in range(N):
        K, L, R = map(int, input().split())
        if L > R:
            favdic_l.setdefault(K, [])
            favdic_l[K].append(L-R)
            total += R
        else:
            favdic_r.setdefault(N-K, [])
            favdic_r[N-K].append(R-L)
            total += L

    for dic in [favdic_l, favdic_r]:
        c_count = 0
        dic = sorted(dic.items())
        for k, v in dic:
            space = k - c_count
            c_count += len(v)
            if space > 0:
                total += sum(v[-space:])
    print(total)
