Buggy python submission (verdict: Time Limit Exceeded):
from bisect import *
from collections import defaultdict
def solve():
  T = int(input())
  ans = [0]*T
  for t in range(T):
    N = int(input())
    left = defaultdict(lambda: [])
    right = defaultdict(lambda: [])
    for i in range(N):
      k,l,r = map(int, input().split())
      if k==N:
        ans[t] += l
      else:
        if l>r:
          insort_right(left[k],l-r)
        elif l<r:
          insort_right(right[k+1],r-l)
        ans[t] += min(l,r)
    for i in range(N,0,-1):
      if len(left[i]):
        ans[t] += left[i][-1]
        for l in left[i][:-1]:
          insort_right(left[i-1],l)
    for i in range(2,N+1):
      if len(right[i]):
        ans[t] += right[i][-1]
        for l in right[i][:-1]:
          insort_right(right[i+1],l)
  return ans
print(*solve(),sep='\n')

Fixed submission:
from bisect import *
from collections import defaultdict
def solve():
  T = int(input())
  ans = [0]*T
  for t in range(T):
    N = int(input())
    left = defaultdict(lambda: [])
    right = defaultdict(lambda: [])
    for i in range(N):
      k,l,r = map(int, input().split())
      if k==N:
        ans[t] += l
      else:
        if l>r:
          insort_right(left[k],l-r)
        elif l<r:
          insort_right(right[k+1],r-l)
        ans[t] += min(l,r)
    lis = []
    for i in range(N,0,-1):
      for l in left[i]:
        insort_right(lis,l)
      if len(lis):
        ans[t] += lis.pop(-1)
    lis = []
    for i in range(2,N+1):
      for r in right[i]:
        insort_right(lis,r)
      if len(lis):
        ans[t] += lis.pop(-1)
  return ans
print(*solve(),sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import *
from collections import defaultdict
def solve():
  T = int(input())
  ans = [0]*T
  for t in range(T):
    N = int(input())
    left = defaultdict(lambda: [])
    right = defaultdict(lambda: [])
    for i in range(N):
      k,l,r = map(int, input().split())
      if k==N:
        ans[t] += l
      else:
        if l>r:
          insort_right(left[k],l-r)
        elif l<r:
          insort_right(right[k+1],r-l)
        ans[t] += min(l,r)
    for i in range(N,0,-1):
      if len(left[i]):
        ans[t] += left[i][-1]
        for l in left[i][:-1]:
          insort_right(left[i-1],l)
    for i in range(2,N+1):
      if len(right[i]):
        ans[t] += right[i][-1]
        for l in right[i][:-1]:
          insort_right(right[i+1],l)
  return ans
print(*solve(),sep='\n')

Fixed submission:
from bisect import *
from collections import defaultdict
def solve():
  T = int(input())
  ans = [0]*T
  for t in range(T):
    N = int(input())
    left = defaultdict(lambda: [])
    right = defaultdict(lambda: [])
    for i in range(N):
      k,l,r = map(int, input().split())
      if k==N:
        ans[t] += l
      else:
        if l>r:
          insort_right(left[k],l-r)
        elif l<r:
          insort_right(right[k+1],r-l)
        ans[t] += min(l,r)
    lis = []
    for i in range(N,0,-1):
      for l in left[i]:
        insort_right(lis,l)
      if len(lis):
        ans[t] += lis.pop(-1)
    lis = []
    for i in range(2,N+1):
      for r in right[i]:
        insort_right(lis,r)
      if len(lis):
        ans[t] += lis.pop(-1)
  return ans
print(*solve(),sep='\n')

Buggy python submission (verdict: Runtime Error):
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
      elif l>r:
        left.append([k,l-r])
        ans[t] += r
      elif l<r:
        right.append([k,r-l])
        ans[t] += l
      else:
        ans[t] += l
    left.sort(key = lambda x:(-x[0],-x[1]))
    right.sort(key = lambda x:(x[0],-x[1]))
    now = left[0][0]
    ans[t] += left[0][1]
    for i in range(1,len(left)):
      while left[i][0]>=now:
        left[i][0] -= 1
      ans[t] += left[i][1]
      now -= 1
      if now==0:
        break
    now = right[0][1]
    for i in range(1,len(left)):
      while right[i][0]<=now:
        right[i][0] -= 1
      ans[t] += right[i][1]
      now -= 1
      if now==0:
        break
  return ans
print(*solve(),sep='\n')

Fixed submission:
from bisect import *
from collections import defaultdict
def solve():
  T = int(input())
  ans = [0]*T
  for t in range(T):
    N = int(input())
    left = defaultdict(lambda: [])
    right = defaultdict(lambda: [])
    for i in range(N):
      k,l,r = map(int, input().split())
      if k==N:
        ans[t] += l
      else:
        if l>r:
          insort_right(left[k],l-r)
        elif l<r:
          insort_right(right[k+1],r-l)
        ans[t] += min(l,r)
    lis = []
    for i in range(N,0,-1):
      for l in left[i]:
        insort_right(lis,l)
      if len(lis):
        ans[t] += lis.pop(-1)
    lis = []
    for i in range(2,N+1):
      for r in right[i]:
        insort_right(lis,r)
      if len(lis):
        ans[t] += lis.pop(-1)
  return ans
print(*solve(),sep='\n')