module mod_priority_queue
  implicit none
  integer, private, parameter :: intkind = 4
  type priority_queue
    integer :: size = 0
    integer(intkind), private, allocatable :: heap(:)
  contains
    procedure :: offer => offer
    procedure :: clear => clear
    procedure :: poll => poll
    procedure :: peek => peek
  end type priority_queue
contains
  logical function less(a, b) result(res)
    integer(intkind), intent(in) :: a, b
    res = a < b
  end
  subroutine swap(a, b)
    integer(intkind), intent(inout) :: a, b
    a = xor(a, b)
    b = xor(a, b)
    a = xor(a, b)
  end
  subroutine offer(this, item)
    class(priority_queue), intent(inout) :: this
    integer(intkind), intent(in) :: item
    integer :: n, i
    integer(intkind), allocatable :: tmp(:)
    if (.not.allocated(this%heap)) allocate(this%heap(4))
    if (this%size == size(this%heap)) then
      allocate(tmp(this%size))
      tmp = this%heap
      deallocate(this%heap)
      allocate(this%heap(2 * this%size))
      this%heap(1:this%size) = tmp
    end if
    this%size = this%size + 1
    this%heap(this%size) = item
    n = this%size
    do while (n > 1)
      i = n / 2
      if (less(this%heap(n), this%heap(i))) then
        call swap(this%heap(n), this%heap(i))
      end if
      n = i
    end do
  end
  subroutine clear(this)
    class(priority_queue), intent(inout) :: this
    if (allocated(this%heap)) deallocate(this%heap)
    this%size = 0
  end
  integer(intkind) function poll(this) result(res)
    class(priority_queue), intent(inout) :: this
    integer :: n, i, j
    n = this%size
    res = this%heap(1)
    this%heap(1) = this%heap(n)
    this%size = this%size - 1
    i = 1
    do while (2 * i < n)
      j = 2 * i
      if (j + 1 < n) then
        if (less(this%heap(j + 1), this%heap(j))) j = j + 1
      end if
      if (less(this%heap(j), this%heap(i))) then
        call swap(this%heap(j), this%heap(i))
      end if
      i = j
    end do
  end
  integer(intkind) function peek(this) result(res)
    class(priority_queue), intent(in) :: this
    res = this%heap(1)
  end
end module mod_priority_queue
module mod_two_dim_merge_sort
  implicit none
  integer, private, parameter :: intkind = 4
contains
  logical function less(a, b) result(res)
    integer(intkind), intent(in) :: a(:), b(:)
    res = a(1) < b(1) .or. (a(1) == b(1) .and. a(2) > b(2)) .or. &
    (a(1) == b(1) .and. a(2) == b(2) .and. a(3) <= b(3))
  end
  subroutine merge_sort(a)
    integer(intkind), intent(inout) :: a(:, :)
    integer :: n, m, l, i, u
    n = size(a, 1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m / 2
        u = min(2 * i * l, n)
        call merger(a(2 * (i - 1) * l + 1:(2 * i - 1) * l, :), &
        a((2 * i - 1) * l + 1:u, :))
      end do
      l = 2 * l
      m = (m + 1) / 2
    end do
  end
  subroutine merger(a1, a2)
    integer(intkind), intent(inout) :: a1(:, :), a2(:, :)
    integer(intkind) :: a(size(a1, 1) + size(a2, 1), size(a1, 2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1,1)
    n2 = size(a2,1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (less(a1(i1, :), a2(i2, :))) then
        a(i1 + i2 - 1, :) = a1(i1, :)
        i1 = i1 + 1
      else
        a(i1 + i2 - 1, :) = a2(i2, :)
        i2 = i2 + 1
       end if
    end do
    if (i1 <= n1) then
      a(i1 + i2 - 1:n1 + n2, :) = a1(i1:n1, :)
    else
      a(i1 + i2 - 1:n1 + n2, :) = a2(i2:n2, :)
    end if
    a1 = a(1:n1, :)
    a2 = a(n1 + 1:n1 + n2, :)
  end
end module mod_two_dim_merge_sort
program camel_train
  use mod_priority_queue
  use mod_two_dim_merge_sort
  implicit none
  integer :: t, loop, n, c(200000, 3) = 0, i, k, tmp
  integer(8) :: x
  type(priority_queue) :: pq
  read(*,*) t
  do loop = 1, t
    read(*,*) n
    do i = 1, n
      read(*,*) c(i, 1:3)
    end do
    call merge_sort(c(1:n, :))
    x = 0
    do i = 1, n
      x = x + min(c(i, 2), c(i, 3))
    end do
    i = 1
    do k = 1, n
      do while (i <= n .and. c(i, 1) == k)
        if (c(i, 2) >= c(i, 3)) call pq%offer(c(i, 2) - c(i, 3))
        i = i + 1
      end do
      do while (pq%size > k)
        tmp = pq%poll()
      end do
    end do
    do while (pq%size > 0)
      x = x + pq%poll()
    end do
    i = n
    do k = n, 1, -1
      do while (i >= 1 .and. c(i, 1) == k)
        if (c(i, 2) < c(i, 3)) call pq%offer(c(i, 3) - c(i, 2))
        i = i - 1
      end do
      do while (pq%size > n - k)
        tmp = pq%poll()
      end do
    end do
    do while (pq%size > 0)
      x = x + pq%poll()
    end do
    write(*,'(i0)') x
  end do
end program camel_train

Measured memory_bytes: 7956