t = int(input())

for i in range(t):
  n = int(input())  # nは入力回数
  arr = [list(map(int, input().split())) for _ in range(n)]
  
  lb = [i for i in arr if i[1] > i[2]]
  rb = [i for i in arr if i[1] < i[2]]
  lb.sort(key = lambda x: x[1], reverse = True)
  rb.sort(key = lambda x: x[2])
  
  apparr = rb + lb
  print(sum(apparr))