class Queue {
  private var nums: [Int] = []
  func enqueue(_ v: Int) {
    nums.append(v)
    buildFromBottom()
  }
  func dequeue() -> Int {
    let v = nums[0]
    nums.swapAt(0, nums.count - 1)
    nums.remove(at: nums.count - 1)
    buildFromTop()
    return v
  }
  
  func count() -> Int {
    return nums.count
  }
  
  func buildFromTop() {
    var current = 0
    while current < nums.count {
      var target = current
      let left = current * 2 + 1
      let right = left + 1
      if left < nums.count && nums[left] > nums[target] {
        target = left
      }
      if right < nums.count && nums[right] > nums[target] {
        target = right
      }
      if target == current {
        break
      }
      nums.swapAt(target, current)
      current = target
    }
  }
  
  func buildFromBottom() {
    var current = nums.count - 1
    var parent = (current - 1) / 2
    while parent >= 0 {
      if nums[parent] < nums[current] {
        nums.swapAt(parent, current)
        current = parent
        parent = (current - 1) / 2
      } else {
        break
      }
    }
  }
}

func f(_ list: [[Int]]) -> Int {
  let queue = Queue()
  var sum = 0
  for i in (1..<list.count).reversed() {
    list[i].forEach { queue.enqueue($0) }
    if queue.count() > 0 {
      sum += queue.dequeue()
    }
  }
  return sum
}

let T = Int(readLine()!)!

for t in 0..<T {
  let N = Int(readLine()!)!
  var sum = 0
  var left: [[Int]] = .init(repeating: [], count: N + 1)
  var right: [[Int]] = .init(repeating: [], count: N + 1)
  for i in 1...N {
    let inp = readLine()!.split(separator: " ").map { Int($0)! }
    let k = inp[0]
    let l = inp[1]
    let r = inp[2]
    sum += min(l, r)
    if l > r {
      left[k].append(l - min(l, r))
    } else {
      right[N - k].append(r - min(l, r))
    }
  }
  sum += f(left)
  sum += f(right)
  print(sum)
}
