#![allow(
    non_snake_case,
    unused_variables,
    unused_assignments,
    unused_mut,
    unused_imports,
    dead_code
)]
//use proconio::fastout;
use proconio::input;
use proconio::marker::*;
use std::cmp::*;
use std::collections::*;
//use std::collections::VecDeque;

fn main() {
    input! {
       T:usize
    }

    for z in 0..T {
        input! {
            N:usize,
             mut KLR:[(Usize1,i64,i64);N]
        }
        let mut ans = 0;
        let mut KL = BTreeSet::new();
        let mut KR = BTreeSet::new();
        for &(K, L, R) in KLR.iter() {
            if L >= R {
                KL.insert((L - R, K, L, R));
            } else {
                KR.insert((R - L, K, L, R));
            }
        }
        let n = KL.len();
        let mut map = HashMap::new();
        'out1: for &(LR, K, L, R) in KL.iter() {
            //println!("L-R={},K={},L={},R={}", LR, K, L, R);
            let mut x = min(K, n - 1);
            while map.contains_key(&x) {
                if x > 0 {
                    x -= 1;
                } else {
                    //      println!("+{}", R);
                    ans += R;
                    continue 'out1;
                }
            }
            //println!("+{}", L);
            map.insert(x, L);
            ans += if x <= K { L } else { R };
        }
        'out2: for &(RL, K, L, R) in KR.iter() {
            //println!("diff={},K={},L={},R={}", RL, K, L, R);
            let mut x = min(max(K + 1, n), N - 1);
            while map.contains_key(&x) {
                if x < N - 1 {
                    x += 1;
                } else {
                    //        println!("+{}", L);
                    ans += L;
                    continue 'out2;
                }
            }
            //println!("+{}", R);
            map.insert(x, R);
            ans += if x <= K { L } else { R };
        }
        //println!("{:?}", map);
        println!("{}", ans);
    }
}
