use proconio::{input, fastout};
use std::collections::BinaryHeap;
use std::cmp::Reverse;

#[fastout]
fn main() {
    input! {
        t: usize,
    }

    let mut cases = vec![];

    for _ in 0..t {
        input! {
            n: usize,
            klr: [(usize, usize, usize); n],
        }
        let mut left = BinaryHeap::new();
        let mut right = BinaryHeap::new();

        let mut left_sum = 0;
        let mut right_sum = 0;

        for (k, l, r) in klr {
            if l >= r {
                left_sum += r;
                left.push((Reverse(k - 1), l - r));
            } else {
                right_sum += l;
                right.push((Reverse(n - k), r - l));
            }
        }

        cases.push((left, left_sum, right, right_sum));
    }

    for (mut left, mut left_sum, mut right, mut right_sum) in cases {
        let mut index = 0;
        while let Some((Reverse(k), value)) = left.pop() {
            if k < index {
                continue;
            }
            left_sum += value;
            index += 1;
        }
        let mut index = 0;
        while let Some((Reverse(k), value)) = right.pop() {
            if k <= index {
                continue;
            }
            right_sum += value;
            index += 1;
        }
        println!("{}", left_sum + right_sum);
    }
}
