def solve():
  T = int(input())
  ans = [0]*T
  for t in range(T):
    N = int(input())
    left = []
    right = []
    for i in range(N):
      k,l,r = map(int, input().split())
      if k==N:
        ans[t] += l
      else:
        if l>r:
          left.append([k,l-r])
        elif l<r:
          right.append([k+1,r-l])
        ans[t] += min(l,r)
    left.sort(key = lambda x:(-x[0],-x[1]))
    right.sort(key = lambda x:(x[0],-x[1]))
    if len(left):
      now = N+1
      for i in range(len(left)):
        ans[t] += left[i][1]
        now = min(now-1,left[i][0])
        if now==1:
          break
    if len(right):
      now = 0
      for i in range(len(right)):
        ans[t] += right[i][1]
        now = max(now+1,right[i][0])
        if now==N:
          break
  return ans
print(*solve(),sep='\n')