You are given a group of camels numbered from 1 to N. Snuke wants to arrange them in a row to maximize their total happiness. Each camel's happiness is determined by its position in the row. For each test case, you need to find the maximum total happiness of the camels. The input consists of T test cases, and the output should contain the answer for each test case. The constraints are that all input values are integers, T is between 1 and 10^5, N is between 1 and 2*10^5, and the sum of N values in each input file is at most 2*10^5. The input format includes the number of test cases followed by the details of each case, and the output format requires printing the answer for each test case.

# Buggy version:
import heapq
T=int(input())

for _ in range(T):
  N=int(input())
  
  llist=[]
  rlist=[]
  base=0
  for _ in range(N):
    K,L,R=map(int,input().split())    
    if L>R:
      llist.append((K,L-R))
      base+=R
    elif L<R:
      rlist.append((N-K,R-L))
      base+=L
      
  llist.sort()
  rlist.sort()      
  #print(base)
  #print(llist)
  #print(rlist)
  
  hq_left=[]
  for i,l in llist:
    heapq.heappush(hq_left,l)
    if len(hq_left)>i:
      heapq.heappop(hq_left)
  hq_right=[]
  for i,r in rlist:
    heapq.heappush(hq_right,r)
    if len(hq_right)>i:
      heapq.heappop(hq_right)
      
  answer=base
  while hq_left:
    answer+=heapq.heappop(hq_left)
  while hq_right:
    answer+=heapq.heappop(hq_right)
    
  print(answer)

# Fixed version:
import heapq
T=int(input())

for _ in range(T):
  N=int(input())
  
  llist=[]
  rlist=[]
  base=0
  for _ in range(N):
    K,L,R=map(int,input().split())
    base+=min(L,R)
    if L>R:
      llist.append((K,L-R))
    elif L<R:
      rlist.append((N-K,R-L))
      
  llist.sort()
  rlist.sort()      
  #print(base)
  #print(llist)
  #print(rlist)
  
  hq_left=[]
  for i,l in llist:
    heapq.heappush(hq_left,l)
    if len(hq_left)>i:
      heapq.heappop(hq_left)
  hq_right=[]
  for i,r in rlist:
    heapq.heappush(hq_right,r)
    if len(hq_right)>i:
      heapq.heappop(hq_right)
      
  answer=base
  while hq_left:
    answer+=heapq.heappop(hq_left)
  while hq_right:
    answer+=heapq.heappop(hq_right)
    
  print(answer)

# Buggy version:
t=int(input())
import heapq
for _ in range(t):
    n=int(input())
    L=[];R=[]
    ans=0
    for _ in range(n):
        ki,li,ri = map(int, input().split())
        dif=li-ri
        if dif>=0:
            L.append((ki-1,-dif))
            ans+=ri
        else:
            R.append((n-ki-1,dif))
            ans+=li
    L.sort();R.sort()
    hL=[]
    hR=[]
    for i in range(len(L)):
        if L[i][0]>len(hL):
            heapq.heappush(hL,L[i][1])
        elif L[i][0]==len(hL):
            heapq.heappushpop(hL,L[i][1])
    for i in range(len(R)):
        if R[i][0]>len(hR):
            heapq.heappush(hR,R[i][1])
        elif L[i][0]==len(hR):
            heapq.heappushpop(hR,R[i][1])
    print(ans-sum(hL)-sum(hR))        
    
    

# Fixed version:
t=int(input())
import heapq
for _ in range(t):
    n=int(input())
    L=[];R=[]
    ans=0
    for _ in range(n):
        ki,li,ri = map(int, input().split())
        dif=li-ri
        if dif>=0:
            L.append((ki-1,dif))
            ans+=ri
        else:
            R.append((n-ki-1,-dif))
            ans+=li
    L.sort();R.sort()
    hL=[]
    hR=[]
    for i in range(len(L)):
        if L[i][0]>=len(hL):
            heapq.heappush(hL,L[i][1])
        elif L[i][0]==len(hL)-1:
            heapq.heappushpop(hL,L[i][1])
    for i in range(len(R)):
        if R[i][0]>=len(hR):
            heapq.heappush(hR,R[i][1])
        elif R[i][0]==len(hR)-1:
            heapq.heappushpop(hR,R[i][1])
    print(ans+sum(hL)+sum(hR))

# Buggy version:
#!python3

import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())
from heapq import heappush, heappushpop

def resolve():
    it = map(int, sys.stdin.read().split())

    T = next(it)
    ans = []
    for i in range(T):
        N = next(it)

        val = 0
        a1 = [[] for i in range(N)];
        a2 = [[] for i in range(N)];
        for i, v1, v2 in ((next(it)-1, next(it), next(it)) for i in range(N)):
            diff = v1 - v2
            if diff == 0:
                val += v1
            elif diff > 0:
                a1[i].append(diff)
                val += v2
            else:
                i = N-1-i
                a2[i].append(-diff)
                val += v1

        for ax in (a1, a2):
            dq = []
            for i, aa in enumerate(ax):
                if not aa: continue
                for diff in aa:
                    ld = len(dq)
                    if ld <= i:
                        heappush(dq, diff)
                    else:
                        diff = heappushpop(dq, diff)

            val += sum(dq)

        ans.append(val)


    print(*ans, sep="\n")



if __name__ == "__main__":
    resolve()


# Fixed version:
#!python3

import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())
from heapq import heappush, heappushpop

def resolve():
    it = map(int, sys.stdin.read().split())

    T = next(it)
    ans = []
    for i in range(T):
        N = next(it)

        val = 0; a1 = []; a2 = []
        for i, v1, v2 in ((next(it), next(it), next(it)) for i in range(N)):
            diff = v1 - v2
            if diff == 0:
                val += v1
            elif diff > 0:
                a1.append((i, diff))
                val += v2
            else:
                a2.append((N-i, -diff))
                val += v1
        a1.sort(); a2.sort()

        for aa in (a1, a2):
            dq = []
            for i, diff in aa:
                ld = len(dq)
                if ld < i:
                    heappush(dq, diff)
                else:
                    diff = heappushpop(dq, diff)

            val += sum(dq)

        ans.append(val)


    print(*ans, sep="\n")



if __name__ == "__main__":
    resolve()

# Buggy version:
from heapq import*
i=input
for _ in'_'*int(i()):
 n,s,x,*y=int(i()),0,[]
 for _ in'_'*n:k,l,r=t=[*map(int,i().split())];x+=[t]*(l>r);y+=[(n-k,r,l)]*(l<r)
 for x in x,y:
  x.sort();n,*h=len(x),
  while n:
   n-=1
   while x and x[-1][0]>n:k,l,r=x.pop();heappush(h,(r-l,l,r))
   if h:s+=heappop(h)[1]
  for*_,r in x+h:s+=r
 print(s)

# Fixed version:
from heapq import*
i=input
for _ in'_'*int(i()):
 n,s,x,*y=int(i()),0,[]
 for _ in'_'*n:
  k,l,r=map(int,i().split())
  if l>r:x+=(k,l,r),
  else:y+=(n-k,r,l),
 for x in(x,y):
  x.sort();n,*h=len(x),
  while n:
   n-=1
   while x and x[-1][0]>n:k,l,r=x.pop();heappush(h,(r-l,l,r))
   if h:s+=heappop(h)[1]
  for*_,r in x+h:s+=r
 print(s)

# Buggy version:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop,heappush

t = int(readline())
q = list(map(int,read().split()))

now = 0
ans = [0] * t
for cyc in range(t):
    n = q[now]
    klr = q[now+1: now+1+3*n]
    now = now+1+3*n

    it = iter(klr)
    lc = []
    rc = []
    min_p = 0
    for x,y,z in zip(it,it,it):
        if(y==z):
            min_p += y
        elif(y>z):
            min_p += z
            lc.append((x,y-z))
        else:
            min_p += y
            rc.append((n-x,z-y))

    res = min_p
    lc.sort()
    rc.sort()
    print(lc)

    for cam in [lc,rc]:
        hq = []
        for i in range(n,0,-1):
            if(len(cam)==0):
                continue
            while(cam[-1][0] == i):
                x,p = cam.pop()
                heappush(hq,p*-1)
                if(len(cam)==0):
                    break
            if(hq):
                res += heappop(hq)*-1

    ans[cyc] = res

print('\n'.join(map(str,ans)))

# Fixed version:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop,heappush

t = int(readline())
q = list(map(int,read().split()))

now = 0
ans = [0] * t
for cyc in range(t):
    n = q[now]
    klr = q[now+1: now+1+3*n]
    now = now+1+3*n

    it = iter(klr)
    lc = []
    rc = []
    min_p = 0
    for x,y,z in zip(it,it,it):
        if(y==z):
            min_p += y
        elif(y>z):
            min_p += z
            lc.append((x,y-z))
        else:
            min_p += y
            rc.append((n-x,z-y))

    res = min_p
    lc.sort()
    rc.sort()

    for cam in [lc,rc]:
        hq = []
        for i in range(n,0,-1):
            if(len(cam)>0):
                while(cam[-1][0] == i):
                    x,p = cam.pop()
                    heappush(hq,p*-1)
                    if(len(cam)==0):
                        break
            if(hq):
                res += heappop(hq)*-1

    ans[cyc] = res

print('\n'.join(map(str,ans)))

# Buggy version:
from heapq import heappush, heappop
T = int(input())

def process(arr):
    arr.sort()
    baseS = 0
    diffS = 0
    q = []
    length = 0
    for K, L, R in arr1:
        baseS += R
        diff = L - R
        heappush(q, diff)
        diffS += diff
        length += 1
        while length > K:
            d = heappop(q)
            diffS -= d
            length -= 1
    return baseS + diffS

for _ in range(T):
    N = int(input())
    arr1 = []
    arr2 = []
    for _ in range(N):
        K, L, R = list(map(int, input().split()))
        if L > R:
            arr1.append((K, L, R))
        else:
            arr2.append((N-K, R, L))
    print(process(arr1) + process(arr2))


# Fixed version:
from heapq import heappush, heappop
T = int(input())

for _ in range(T):
    N = int(input())
    arr1 = []
    arr2 = []
    for _ in range(N):
        K, L, R = list(map(int, input().split()))
        if L > R:
            arr1.append((K, L, R))
        else:
            arr2.append((N-K, R, L))

    arr1.sort()
    baseS = 0
    diffS = 0
    q = []
    length = 0
    for K, L, R in arr1:
        baseS += R
        diff = L - R
        heappush(q, diff)
        diffS += diff
        length += 1
        while length > K:
            d = heappop(q)
            diffS -= d
            length -= 1
    ans = baseS + diffS

    arr2.sort()
    baseS = 0
    diffS = 0
    q = []
    length = 0
    for K, L, R in arr2:
        baseS += R
        diff = L - R
        heappush(q, diff)
        diffS += diff
        length += 1
        while length > K:
            d = heappop(q)
            diffS -= d
            length -= 1
    ans += baseS + diffS
    print(ans)