import bisect

t = int(input())
for _ in range(t):
    n = int(input())

    c = [tuple(map(int, input().split())) for _ in range(n)]

    # 前にいる方が嬉しい
    pc = [cc for cc in c if cc[1] >= cc[2]]
    pcl = len(pc)
    pc.sort(key=lambda x: x[0], reverse=True)
    ps = sum([cc[2] for cc in pc])
    pcc = []
    pg = 0
    for i in range(pcl):
        k = pcl-i
        # 条件に当てはまるラクダ
        ci = 0
        for cc in pc:
            if cc[0] < k:
                break
            ci += 1
            bisect.insort(pcc, cc[1]-cc[2])
        pc = pc[ci:]
        if len(pcc) > 0:
            pg += pcc[-1]
            pcc = pcc[:-1]
    # 後ろにいる方が嬉しい
    nc = [cc for cc in c if cc[1] < cc[2]]
    ncl = len(nc)
    nc.sort(key=lambda x: x[0])
    ns = sum([cc[1] for cc in nc])
    ncc = []
    ng = 0
    for i in range(ncl):
        k = n-ncl+i+1
        # 条件に当てはまるラクダ
        ci = 0
        for cc in nc:
            if cc[0] >= k:
                break
            ci += 1
            bisect.insort(ncc, cc[2]-cc[1])
        nc = nc[ci:]
        if len(ncc) > 0:
            ng += ncc[-1]
            ncc = ncc[:-1]
    print(ps+pg+ns+ng)
