#![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;
        KLR.sort_by_key(|(_, L, R)| L - R);
        KLR.reverse();
        let mut KL = Vec::with_capacity(N);
        let mut KR = Vec::with_capacity(N);
        for &(K, L, R) in KLR.iter() {
            if L >= R {
                KL.push((K, L, R));
            } else {
                KR.push((K, L, R));
            }
        }
        let n = KL.len();
        let mut map = HashMap::new();
        for &(K, L, R) in KL.iter() {
            //println!("L-R={},K={},L={},R={}", L - R, K, L, R);
            let mut x = min(K, n - 1);
            while map.contains_key(&x) {
                if x > 0 {
                    x -= 1;
                } else {
                    x = n - 1;
                }
            }
            ans += if x <= K {
                map.insert(x, L);
                L
            } else {
                map.insert(x, R);
                R
            };
        }
        KR.reverse();
        for &(K, L, R) in KR.iter() {
            //println!("diff={},K={},L={},R={}", R - L, 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 {
                    x = n;
                }
            }
            ans += if x <= K {
                map.insert(x, L);
                L
            } else {
                map.insert(x, R);
                R
            };
        }
        //println!("{:?}", map);
        println!("{}", ans);
    }
}
