import sys
input=sys.stdin.readline

N=int(input().rstrip('\n'))
for i in range(N):
  T=int(input().rstrip('\n'))
  rs_p=[]
  rs_n=[]
  rs_a=[]
  dif = []
  ans=[0]*T
  for y in range(T):
    rs = tuple(int(x) for x in input().rstrip('\n').split())
    rs_a.append(rs)
    dif.append(abs(rs[1]-rs[2]))
    if rs[1]>rs[2]:
      rs_p.append(y)
    else:
      rs_n.append(y)
  for x in range(T):
    p = [i for i, x in enumerate(dif) if x == max(dif)]
    q = p[0]
    rn=rs_a[q]
    if q in rs_p:
      for i in range(rn[0]-1,0,-1):
        if ans[i]==0:
          ans[i]=rn[1]
          break
    else:
      for i in range(T,0,-1):
        if ans[i]==0:
          ans[i]=rn[2]
          break
  print(sum(ans))
