import numpy as np
t = int(input())

ans_li = []
for _ in range(t):

  n = int(input())
  left = []
  right = []
  m_value = []
  ans = 0
  l_cnt = 0
  r_cnt = 0
  for i in range(n):
    k,l,r = map(int, input().split())
    if l > r:
      left.append([k,1/(l-r),l,r])
    elif l < r:
      right.append([n-k,1/(r-l),r,l])
    else:
      m_value.append(l)

  left_sorted = sorted(left, key= lambda x: (x[0],x[1]))
  right_sorted = sorted(right, key= lambda x: (x[0],x[1]))

  for l in left_sorted:
    if l_cnt <= l[0]:
      ans += l[2]
    else:
      ans += l[3]

    l_cnt += 1

  for r in right_sorted:
    if r_cnt < r[0]:
      ans += r[2]
    else:
      ans += r[3]

    r_cnt += 1

  ans += sum(m_value)

  ans_li.append(ans)

for i in ans_li:
  print(i)