import heapq



T = int(eval(input()))



for _ in range(T):

  N = int(eval(input()))

  res = 0

  Ls,Rs = [[] for _ in range(N)], [[] for _ in range(N)]

  for _ in range(N):

    K,L,R = list(map(int,input().split()))

    m = min(L,R)

    res += m

    L -= m

    R -= m

    if L>0:

      Ls[K-1].append(L)

    else:

      if K != N:

        Rs[K].append(R)





  pq1 = []

  for i in reversed(list(range(N))):

    for l in Ls[i]:

      heapq.heappush(pq1, -l)

    if pq1:

      p = -heapq.heappop(pq1)

      res += p



  pq2 = []

  for i in range(N):

    for r in Rs[i]:

      heapq.heappush(pq2, -r)

    if pq2:

      p = -heapq.heappop(pq2)

      res += p



  print(res)
import heapq

def solve():

  N = int(eval(input()))

  ans = 0

  left,right = [ [] for i in range(N) ],[ [] for i in range(N) ]

  for _ in range(N):

    k,l,r = list(map(int,input().split()))

    ans += min(l,r)

    if l>r:

      left[k-1].append(l-r)

    else:

      if k >= N: continue

      right[k].append(r-l)



  # left

  pq = []

  for i in range(N-1,-1,-1):

    for l in left[i]:

      heapq.heappush(pq,-l)

    if pq:

      ans += -heapq.heappop(pq)



  # right

  pq = []

  for i in range(N):

    for r in right[i]:

      heapq.heappush(pq,-r)

    if pq:

      ans += -heapq.heappop(pq)



  print(ans)



for i in range(int(eval(input()))):

  solve()
import heapq

T = int(eval(input()))
for _ in range(T):
    N = int(eval(input()))
    res = 0
    Ls, Rs = [[] for _ in range(N)], [[] for _ in range(N)]
    for _ in range(N):
        K, L, R = list(map(int, input().split()))
        m = min(L, R)
        res += m
        L -= m
        R -= m
        if L > 0:
            Ls[K - 1].append(L)
        else:
            if K != N:
                Rs[K].append(R)
    pq1 = []
    for i in reversed(list(range(N))):
        for l in Ls[i]:
            heapq.heappush(pq1, -l)
        if pq1:
            p = -heapq.heappop(pq1)
            res += p
    pq2 = []
    for i in range(N):
        for r in Rs[i]:
            heapq.heappush(pq2, -r)
        if pq2:
            p = -heapq.heappop(pq2)
            res += p
    print(res)

import heapq


def solve():
    N = int(eval(input()))
    ans = 0
    left, right = [[] for i in range(N)], [[] for i in range(N)]
    for _ in range(N):
        k, l, r = list(map(int, input().split()))
        ans += min(l, r)
        if l > r:
            left[k - 1].append(l - r)
        else:
            if k >= N:
                continue
            right[k].append(r - l)
    # left
    pq = []
    for i in range(N - 1, -1, -1):
        for l in left[i]:
            heapq.heappush(pq, -l)
        if pq:
            ans += -heapq.heappop(pq)
    # right
    pq = []
    for i in range(N):
        for r in right[i]:
            heapq.heappush(pq, -r)
        if pq:
            ans += -heapq.heappop(pq)
    print(ans)


for i in range(int(eval(input()))):
    solve()

-T = int(eval(input())) -for _ in range(T): + +def solve(): -    res = 0 -    Ls, Rs = [[] for _ in range(N)], [[] for _ in range(N)] +    ans = 0 +    left, right = [[] for i in range(N)], [[] for i in range(N)] -        K, L, R = list(map(int, input().split())) -        m = min(L, R) -        res += m -        L -= m -        R -= m -        if L > 0: -            Ls[K - 1].append(L) +        k, l, r = list(map(int, input().split())) +        ans += min(l, r) +        if l > r: +            left[k - 1].append(l - r) -            if K != N: -                Rs[K].append(R) -    pq1 = [] -    for i in reversed(list(range(N))): -        for l in Ls[i]: -            heapq.heappush(pq1, -l) -        if pq1: -            p = -heapq.heappop(pq1) -            res += p -    pq2 = [] +            if k >= N: +                continue +            right[k].append(r - l) +    # left +    pq = [] +    for i in range(N - 1, -1, -1): +        for l in left[i]: +            heapq.heappush(pq, -l) +        if pq: +            ans += -heapq.heappop(pq) +    # right +    pq = [] -        for r in Rs[i]: -            heapq.heappush(pq2, -r) -        if pq2: -            p = -heapq.heappop(pq2) -            res += p -    print(res) +        for r in right[i]: +            heapq.heappush(pq, -r) +        if pq: +            ans += -heapq.heappop(pq) +    print(ans) + + +for i in range(int(eval(input()))): +    solve()