module mod_segment_tree
  implicit none
  integer, private, parameter :: intkind = 4
  type segment_tree
    integer(intkind) :: default = -1
    integer :: n, p
    integer(intkind), private, allocatable :: body(:)
    integer, private, allocatable :: idx(:)
  contains
    procedure :: update => update
    procedure :: maxlocation => maxlocation
    procedure :: maxvalue => maxvalue
  end type
  interface segment_tree
    module procedure :: newst0
  end interface segment_tree
contains
  type(segment_tree) function newst0(n, def) result(res)
    integer, intent(in) :: n
    integer(intkind), optional, intent(in) :: def
    integer :: p, i
    p = 1
    do while (p < n)
      p = lshift(p, 1)
    end do
    if (present(def)) res%default = def
    res%n = n
    res%p = p
    allocate(res%body(2 * p - 1))
    res%body = res%default
    allocate(res%idx(2 * p - 1))
    res%idx = 0
    do i = 1, n
      res%idx(i + p - 1) = i
    end do
  end
  subroutine update(this, i, v)
    class(segment_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer(intkind), optional, intent(in) :: v
    integer :: x, y
    x = i + this%p - 1
    this%body(x) = merge(v, this%default, present(v))
    do while (x > 1)
      x = x / 2
      y = merge(2 * x, 2 * x + 1, this%body(2 * x) >= this%body(2 * x + 1))
      this%body(x) = this%body(y)
      this%idx(x) = this%idx(y)
    end do
  end
  integer function maxlocation(this) result(res)
    class(segment_tree), intent(in) :: this
    res = this%idx(1)
  end
  integer(intkind) function maxvalue(this) result(res)
    class(segment_tree), intent(in) :: this
    res = this%body(1)
  end
end module mod_segment_tree
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_two_dim_merge_sort
  use mod_segment_tree
  implicit none
  integer :: t, loop, n, c(200000, 3) = 0, i, j, k
  integer(8) :: x
  logical :: u(200000) = .true.
  type(segment_tree) :: st
  read(*,*) t
  do loop = 1, t
    read(*,*) n
    st = segment_tree(n)
    do i = 1, n
      read(*,*) c(i, 1:3)
    end do
    call merge_sort(c(1:n, :))
    do i = 1, n
      call st%update(i, c(i, 3))
    end do
    u(1:n) = .true.
    i = 1
    k = 1
    x = 0
    do k = 1, n
      do while (i <= n .and. c(i, 1) == k)
        if (u(i)) call st%update(i, c(i, 2))
        i = i + 1
      end do
      j = st%maxlocation()
      x = x + st%maxvalue()
      u(j) = .false.
      call st%update(j)
    end do
    write(*,'(i0)') x
  end do
end program camel_train