t=int(input())
for _ in range(t):
  n=int(input())
  s=0
  list1=[]
  list2=[]
  for i in range(n):
    k,l,r=map(int,input().split())
    s+=min(l,r)
    if l-r>0: list1.append((l-r,k))
    elif k!=n: list2.append((r-l,n-k))

  list1=sorted(list1, reverse=True, key=lambda x: x[0])
  list2=sorted(list2, reverse=True, key=lambda x: x[0])

  dic={p:0 for p in range(1,n+1)}
  for x in list1:
    w=x[1]
    dic[w]+=1
    if dic[w]<=w: s+=x[0]

  dic={p:0 for p in range(1,n+1)}
  for x in list2:
    w=x[1]
    dic[w]+=1
    if dic[w]<=w: s+=x[0]
  print(s)

