Buggy python submission (verdict: Runtime Error):
from heapq import heappop, heappush, heapify

T = int(input())

for _ in range(T):
    N = int(input())
    camel = [tuple(map(int, input().split())) for i in range(N)]
    dif = [(camel[i][1] - camel[i][2], camel[i][0] - 1, i) for i in range(N)]

    pos = []
    neg = []

    for i in range(N):
        if dif[i][0] >= 0:
            pos.append((-dif[i][0], dif[i][1], dif[i][2]))
        else:
            neg.append(dif[i])

    p = len(pos)
    n = len(neg)

    pos.sort(key=lambda x: x[1], reverse=True)
    neg.sort(key=lambda x: x[1])

    heappos = []
    heapneg = []

    arr = [None for i in range(N)]

    j = 0
    for i in range(p)[::-1]:
        f = False
        while j != p and pos[j][1] >= i:
            heappush(heappos, pos[j])
            j += 1
            f = True
        #if not f and j != p:
        #    heappush(heappos, pos[j])
        #    j += 1
        d, k, id = heappop(heappos)
        arr[i] = id

    j = 0
    for i in range(n):
        f = False
        while j != n and neg[j][1] <= p + i:
            heappush(heapneg, neg[j])
            j += 1
            f = True
        #if not f and j != n:
        #    heappush(heapneg, neg[j])
        #    j += 1
        d, k, id = heappop(heapneg)
        arr[p + i] = id
    res = 0

    for i in range(N):
        k, l, r = camel[arr[i]]
        if k >= i + 1:
            res += l
        else:
            res += r

    print(res)

Fixed submission:
from heapq import heappop, heappush, heapify
from collections import deque

T = int(input())

for _ in range(T):
    N = int(input())
    camel = [tuple(map(int, input().split())) for i in range(N)]

    pos = []
    neg = []

    for i in range(N):
        k, l, r = camel[i]
        if l - r >= 0:
            pos.append((r - l, k, i))
        else:
            neg.append((l - r, N - k, i))

    pos.sort(key=lambda x:x[1], reverse=True)
    p = len(pos)

    heappos = []
    respos = [None for i in range(N)]

    j = 0
    for i in range(N)[::-1]:
        while j < p and pos[j][1] == i + 1:
            heappush(heappos, pos[j])
            j += 1
        if heappos:
            d, k, id = heappop(heappos)
            respos[i] = id
    for i in range(N)[::-1]:
        if respos[i] is None and heappos:
            d, k, id = heappop(heappos)
            respos[i] = id


    neg.sort(key=lambda x:x[1], reverse=True)
    n = len(neg)

    heapneg = []
    resneg = [None for i in range(N)]

    j = 0
    for i in range(N)[::-1]:
        while j < n and neg[j][1] == i + 1:
            heappush(heapneg, neg[j])
            j += 1
        if heapneg:
            d, k, id = heappop(heapneg)
            resneg[i] = id
    while j < n:
        heappush(heapneg, neg[j])
        j += 1
    for i in range(N)[::-1]:
        if resneg[i] is None and heapneg:
            d, k, id = heappop(heapneg)
            resneg[i] = id

    respos = [respos[i] for i in range(N) if respos[i] is not None]
    resneg = [resneg[i] for i in range(N) if resneg[i] is not None]
    res = respos + resneg[::-1]

    ans = 0

    for i in range(N):
        k, l, r = camel[res[i]]
        if k >= i + 1:
            ans += l
        else:
            ans += r

    print(ans)

Buggy python submission (verdict: Runtime Error):
from heapq import heappop, heappush, heapify

T = int(input())

for _ in range(T):
    N = int(input())
    camel = [tuple(map(int, input().split())) for _ in range(N)]
    dif = [(camel[i][1] - camel[i][2], camel[i][0] - 1, i) for i in range(N)]
    pos = []
    neg = []
    for i in range(N):
        if dif[i][0] >= 0:
            pos.append((-dif[i][0], dif[i][1], dif[i][2]))
        else:
            neg.append(dif[i])

    p = len(pos)
    n = len(neg)

    pos.sort(key=lambda x: x[1], reverse=True)
    neg.sort(key=lambda x: x[1])

    heappos = []
    heapneg = []

    arr = [None for _ in range(N)]

    j = 0
    f = False
    for i in range(p)[::-1]:
        while j != p and pos[j][1] >= i:
            heappush(heappos, pos[j])
            j += 1
            f = True
        if not f and j != p:
            heappush(heappos, pos[j])
            j += 1
        d, k, id = heappop(heappos)
        arr[i] = id

    j = 0
    f = False
    for i in range(n):
        while j != n and neg[j][1] <= N - i - 1:
            heappush(heapneg, neg[j])
            j += 1
            f = True
        if not f and j != n:
            heappush(heapneg, neg[j])
            j += 1
        d, k, id = heappop(heapneg)
        arr[N - 1 - i] = id

    res = 0

    for i in range(N):
        k, l, r = camel[arr[i]]
        if k <= i:
            res += r
        else:
            res += l

    print(res)

Fixed submission:
from heapq import heappop, heappush, heapify
from collections import deque

T = int(input())

for _ in range(T):
    N = int(input())
    camel = [tuple(map(int, input().split())) for i in range(N)]

    pos = []
    neg = []

    for i in range(N):
        k, l, r = camel[i]
        if l - r >= 0:
            pos.append((r - l, k, i))
        else:
            neg.append((l - r, N - k, i))

    pos.sort(key=lambda x:x[1], reverse=True)
    p = len(pos)

    heappos = []
    respos = [None for i in range(N)]

    j = 0
    for i in range(N)[::-1]:
        while j < p and pos[j][1] == i + 1:
            heappush(heappos, pos[j])
            j += 1
        if heappos:
            d, k, id = heappop(heappos)
            respos[i] = id
    for i in range(N)[::-1]:
        if respos[i] is None and heappos:
            d, k, id = heappop(heappos)
            respos[i] = id


    neg.sort(key=lambda x:x[1], reverse=True)
    n = len(neg)

    heapneg = []
    resneg = [None for i in range(N)]

    j = 0
    for i in range(N)[::-1]:
        while j < n and neg[j][1] == i + 1:
            heappush(heapneg, neg[j])
            j += 1
        if heapneg:
            d, k, id = heappop(heapneg)
            resneg[i] = id
    while j < n:
        heappush(heapneg, neg[j])
        j += 1
    for i in range(N)[::-1]:
        if resneg[i] is None and heapneg:
            d, k, id = heappop(heapneg)
            resneg[i] = id

    respos = [respos[i] for i in range(N) if respos[i] is not None]
    resneg = [resneg[i] for i in range(N) if resneg[i] is not None]
    res = respos + resneg[::-1]

    ans = 0

    for i in range(N):
        k, l, r = camel[res[i]]
        if k >= i + 1:
            ans += l
        else:
            ans += r

    print(ans)

Buggy python submission (verdict: Runtime Error):
from heapq import heappop, heappush, heapify

T = int(input())

for _ in range(T):
    N = int(input())
    camel = [tuple(map(int, input().split())) for _ in range(N)]
    dif = [(camel[i][1] - camel[i][2], camel[i][0] - 1, i) for i in range(N)]
    pos = []
    neg = []
    for i in range(N):
        if dif[i][0] >= 0:
            pos.append((-dif[i][0], dif[i][1], dif[i][2]))
        else:
            neg.append(dif[i])

    p = len(pos)
    n = len(neg)

    pos.sort(key=lambda x: x[1], reverse=True)
    neg.sort(key=lambda x: x[1])

    heappos = []
    heapneg = []

    arr = [None for _ in range(N)]

    j = 0
    f = False
    for i in range(p)[::-1]:
        while j != p and pos[j][1] >= i:
            heappush(heappos, pos[j])
            j += 1
            f = True
        if not f and j != p:
            heappush(heapneg, pos[j])
            j += 1
        d, k, id = heappop(heappos)
        arr[i] = id

    j = 0
    f = False
    for i in range(n):
        while j != n and neg[j][1] <= N - i - 1:
            heappush(heapneg, neg[j])
            j += 1
            f = True
        if not f and j != n:
            heappush(heapneg, neg[j])
            j += 1
        d, k, id = heappop(heapneg)
        arr[N - 1 - i] = id

    res = 0

    for i in range(N):
        k, l, r = camel[arr[i]]
        if k <= i:
            res += r
        else:
            res += l

    print(res)


Fixed submission:
from heapq import heappop, heappush, heapify
from collections import deque

T = int(input())

for _ in range(T):
    N = int(input())
    camel = [tuple(map(int, input().split())) for i in range(N)]

    pos = []
    neg = []

    for i in range(N):
        k, l, r = camel[i]
        if l - r >= 0:
            pos.append((r - l, k, i))
        else:
            neg.append((l - r, N - k, i))

    pos.sort(key=lambda x:x[1], reverse=True)
    p = len(pos)

    heappos = []
    respos = [None for i in range(N)]

    j = 0
    for i in range(N)[::-1]:
        while j < p and pos[j][1] == i + 1:
            heappush(heappos, pos[j])
            j += 1
        if heappos:
            d, k, id = heappop(heappos)
            respos[i] = id
    for i in range(N)[::-1]:
        if respos[i] is None and heappos:
            d, k, id = heappop(heappos)
            respos[i] = id


    neg.sort(key=lambda x:x[1], reverse=True)
    n = len(neg)

    heapneg = []
    resneg = [None for i in range(N)]

    j = 0
    for i in range(N)[::-1]:
        while j < n and neg[j][1] == i + 1:
            heappush(heapneg, neg[j])
            j += 1
        if heapneg:
            d, k, id = heappop(heapneg)
            resneg[i] = id
    while j < n:
        heappush(heapneg, neg[j])
        j += 1
    for i in range(N)[::-1]:
        if resneg[i] is None and heapneg:
            d, k, id = heappop(heapneg)
            resneg[i] = id

    respos = [respos[i] for i in range(N) if respos[i] is not None]
    resneg = [resneg[i] for i in range(N) if resneg[i] is not None]
    res = respos + resneg[::-1]

    ans = 0

    for i in range(N):
        k, l, r = camel[res[i]]
        if k >= i + 1:
            ans += l
        else:
            ans += r

    print(ans)

Buggy python submission (verdict: Runtime Error):
from heapq import heappop, heappush, heapify

T = int(input())

for _ in range(T):
    N = int(input())
    camel = [tuple(map(int, input().split())) for _ in range(N)]
    dif = [(camel[i][1] - camel[i][2], camel[i][0] - 1, i) for i in range(N)]
    pos = []
    neg = []
    for i in range(N):
        if dif[i][0] >= 0:
            pos.append((-dif[i][0], dif[i][1], dif[i][2]))
        else:
            neg.append(dif[i])

    p = len(pos)
    n = len(neg)

    pos.sort(key=lambda x: x[1], reverse=True)
    neg.sort(key=lambda x: x[1])

    heappos = []
    heapneg = []

    arr = [None for _ in range(N)]

    j = 0
    for i in range(p)[::-1]:
        while j != p and pos[j][1] >= i:
            heappush(heappos, pos[j])
            j += 1
        d, k, id = heappop(heappos)
        arr[i] = id

    j = 0
    for i in range(n):
        while j != n and neg[j][1] <= N - i - 1:
            heappush(heapneg, neg[j])
            j += 1
        d, k, id = heappop(heapneg)
        arr[N - 1 - i] = id

    res = 0

    for i in range(N):
        k, l, r = camel[arr[i]]
        if k <= i:
            res += r
        else:
            res += l

    print(res)

Fixed submission:
from heapq import heappop, heappush, heapify
from collections import deque

T = int(input())

for _ in range(T):
    N = int(input())
    camel = [tuple(map(int, input().split())) for i in range(N)]

    pos = []
    neg = []

    for i in range(N):
        k, l, r = camel[i]
        if l - r >= 0:
            pos.append((r - l, k, i))
        else:
            neg.append((l - r, N - k, i))

    pos.sort(key=lambda x:x[1], reverse=True)
    p = len(pos)

    heappos = []
    respos = [None for i in range(N)]

    j = 0
    for i in range(N)[::-1]:
        while j < p and pos[j][1] == i + 1:
            heappush(heappos, pos[j])
            j += 1
        if heappos:
            d, k, id = heappop(heappos)
            respos[i] = id
    for i in range(N)[::-1]:
        if respos[i] is None and heappos:
            d, k, id = heappop(heappos)
            respos[i] = id


    neg.sort(key=lambda x:x[1], reverse=True)
    n = len(neg)

    heapneg = []
    resneg = [None for i in range(N)]

    j = 0
    for i in range(N)[::-1]:
        while j < n and neg[j][1] == i + 1:
            heappush(heapneg, neg[j])
            j += 1
        if heapneg:
            d, k, id = heappop(heapneg)
            resneg[i] = id
    while j < n:
        heappush(heapneg, neg[j])
        j += 1
    for i in range(N)[::-1]:
        if resneg[i] is None and heapneg:
            d, k, id = heappop(heapneg)
            resneg[i] = id

    respos = [respos[i] for i in range(N) if respos[i] is not None]
    resneg = [resneg[i] for i in range(N) if resneg[i] is not None]
    res = respos + resneg[::-1]

    ans = 0

    for i in range(N):
        k, l, r = camel[res[i]]
        if k >= i + 1:
            ans += l
        else:
            ans += r

    print(ans)