def solve():
  n = int(input())
  a = [-float("inf") for _ in range(n+1)]
  b = [0 for _ in range(n+1)]
  c = [0 for _ in range(n+1)]
  for _ in range(n):
    k,l,r = map(int,input().split())
    c[k] += 1
    if a[k] == -float("inf"):
      a[k] = l-r
    else:
      if l-r > a[k]:
        a[k] = l-r
    b[k] += r
  s = sum(b)
  e = 0
  f = 0
  m1 = -float("inf")
  m2 = float("inf")
  for i in range(1,n+1):
    if a[i] != -float("inf"):
      if a[i] < 0:
        e += c[i]
        f += 1
        if a[i] > m1:
          m1 = a[i]
      else:
        if m2 > a[i]:
          m2 = a[i] 
    else:
      f += 1
  print(e,f,m1,m2)
  if e >= f:
    for i in range(1,n+1):
       if a[i] >= 0:
          s += b[i]
    print(s)
    if m1+m2 <= 0:
      s -= m2
    else:
      s += m1
  else:
    for i in range(1,n+1):
      if a[i] >= 0:
        s += b[i]
    print(s)
t = int(input())
for _ in range(t):
  solve()