package main
import (
  "bufio"
  "os"
  "strconv"
  "strings"
  "fmt"
  "sort"
)

type Evaluator func(a, b interface{}) interface{}
func Max(a, b interface{}) interface{} {
  if a.(int) < b.(int) { return b }
  return a
}
func Min(a, b interface{}) interface{} {
  if a.(int) < b.(int) { return a }
  return b
} 
type SegmentTree struct {
  size  int
  data  []interface{}
  inf   interface{}
  eval  Evaluator
}
func InitSegmentTree(N int, inf interface{}, eval Evaluator) *SegmentTree {
  size := 1
  for size < N {
    size *= 2
  }
  S := make([]interface{}, size * 2)
  for i := range S {
    S[i] = inf
  }
  return &SegmentTree{
    size: size,
    data: S,
    inf:  inf,
    eval: eval,
  }
}
func (ST *SegmentTree) Update(index int, value interface{}) {
  idx := ST.size + index
  ST.data[idx] = value
  for idx > 1 {
    parent := idx / 2
    ST.data[parent] = ST.eval(ST.data[2 * parent], ST.data[2 * parent + 1])
    idx = parent
  }
}
func (ST *SegmentTree) Get(from, to, index, left, right int) interface{} {
  // get [from, to)
  if to <= left || right <= from { return ST.inf }
  if from <= left && right <= to { return ST.data[index] }
  m := (left + right) / 2
  lval := ST.Get(from, to, 2 * index, left, m)
  rval := ST.Get(from, to, 2 * index + 1, m, right)
  return ST.eval(lval, rval)
}
func (ST *SegmentTree) Query(from, to int) interface{} {
  return ST.Get(from, to, 1, 0, ST.size)
}

type Item struct {
  K int
  L int
  R int
}
type Idx []Item
func (I Idx) Len() int { return len(I) }
func (I Idx) Less(i, j int) bool {
  return AbsInt(I[i].L - I[i].R) < AbsInt(I[j].L - I[j].R)
}
func (I Idx) Swap(i, j int) { I[i], I[j] = I[j], I[i] }

func Solve() {
  var T, N, k, r, l, p, ans int
  NextInt(&T)
  var I, J []Item
  var ST1, ST2 *SegmentTree
  for t := 0; t < T; t++ {
    NextInt(&N)
    I = make([]Item, 0)
    J = make([]Item, 0)
    for i := 0; i < N; i++ {
      NextInt(&k, &l, &r)
      if l >= r {
        I = append(I, Item{k, l, r})
      } else {
        J = append(J, Item{k, l, r})
      }
    }
    sort.Sort(sort.Reverse(Idx(I)))
    sort.Sort(sort.Reverse(Idx(J)))
    ST1 = InitSegmentTree(N + 1, 0, Max)
    ST2 = InitSegmentTree(N + 1, N + 1, Min)
    for i := 1; i <= N; i++ {
      ST1.Update(i, i)
      ST2.Update(i, i)
    }
    ans = 0
    for _, i := range I {
      p = ST1.Query(0, i.K + 1).(int)
      if p > 0 {
        ans += i.L
        ST1.Update(p, -1)
      } else {
        ans += i.R
      }
    }
    for _, j := range J {
      p = ST2.Query(j.K + 1, N + 2).(int)
      if p <= N {
        ans += j.R
        ST2.Update(p, N + 1)
      } else {
        ans += j.L
      }
    }
    Write(ans)
  }
}

func main() {
  Solve()
  Output()
}

var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  buffer := make([]byte, 0)
  for true {
    line, isPrefix, err := reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
    if !isPrefix { break }
  }
  return string(buffer)
}
func NextInt(A ...*int) {
  L := strings.Split(NextLine(), " ")
  for i, a := range A {
    *a, _ = strconv.Atoi(L[i])
  }
}
func NextIntSlice(A *[]int) {
  L := strings.Split(NextLine(), " ")
  (*A) = make([]int, len(L))
  for i, l := range L {
    (*A)[i], _ = strconv.Atoi(l)
  }
}
func NextFloat(A ...*float64) {
  L := strings.Split(NextLine(), " ")
  for i, a := range A {
    *a, _ = strconv.ParseFloat(L[i], 64)
  }
}
func NextFloatSlice(A *[]float64) {
  L := strings.Split(NextLine(), " ")
  (*A) = make([]float64, len(L))
  for i, l := range L {
    (*A)[i], _ = strconv.ParseFloat(l, 64)
  }
}
func Write(S ...interface{}) {
  fmt.Fprintln(writer, S...)
}
func WriteIntSlice(A []int) {
  B := make([]interface{}, len(A))
  for i, a := range A { B[i] = a }
  Write(B...)
}
func Output() {
  _ = writer.Flush()
}
func MaxInt(A ...int) int {
  max := A[0]
  for _, a := range A {
    if max < a { max = a }
  }
  return max
}
func MinInt(A ...int) int {
  min := A[0]
  for _, a := range A {
    if a < min { min = a }
  }
  return min
}
func SumInt(A ...int) int {
  sum := 0
  for _, a := range A {
    sum += a
  }
  return sum
}
func AbsInt(x int) int {
  if x < 0 { x = -x }
  return x
}

Measured memory_bytes: 35480