T = int(input())
from collections import deque
for _ in range(T):
  N = int(input())
  KLR = [list(map(int,input().split())) for _ in range(N)]
  left = deque([])
  right = deque([])
  mid = deque([])
  for K,L,R in KLR:
    x = L - R
    if x==0:
      mid.append([K-1,L,R,x])
    elif x>0:
      left.append([K-1,L,R,x])
    else:
      right.append([K-1,L,R,x])

  left = sorted(left,key = lambda x:x[3],reverse = True)
  right = sorted(right, key = lambda x:x[3])
  flag = [False] * (N)
  ans = 0
  for i in range(len(left)):
    klr = left[i]
    x = klr[0]
    while True:
      if x<0:
        ans += klr[2]
        break
      if flag[x]:
        x -= 1
      else:
        flag[x] = True
        ans += klr[1]
        break
  flag = [False] * (N)
  for i in range(len(right)):
    klr = right[i]
    x = klr[0] + 1
    while True:
      if x>=N:
        ans += klr[1]
        break
      if flag[x]:
        x += 1
      else:
        flag[x] = True
        ans += klr[2]
        break
  for i in range(len(mid)):
    ans += mid[i][1]
    
  print(ans)