use proconio::input;

fn main() {
    input!(t: usize);
    for _ in 0..t {
        input!(n: usize, klr: [(usize, i64, i64); n]);
        let mut ans = klr
            .iter()
            .map(|&(k, l, r)| if k == n { l } else { l.min(r) })
            .sum::<i64>();

        let mut left: Vec<_> = klr
            .iter()
            .filter(|&&(k, l, r)| k != n && l > r)
            .map(|&(k, l, r)| (l - r, k))
            .collect();
        left.sort_by_key(|&(x, _)| std::cmp::Reverse(x));

        let mut right: Vec<_> = klr
            .iter()
            .filter(|&&(k, l, r)| k != n && l < r)
            .map(|&(k, l, r)| (r - l, n - k))
            .collect();
        right.sort_by_key(|&(x, _)| std::cmp::Reverse(x));

        for seq in &[left, right] {
            let mut seg = vec![0; 2 * n];
            for &(d, k) in seq {
                // fold(0, k)
                let fold = {
                    let mut l = n;
                    let mut r = k + n;
                    let mut fold = 0;
                    while l < r {
                        if l % 2 == 1 {
                            fold += seg[l];
                            l += 1;
                        }
                        if r % 2 == 1 {
                            r -= 1;
                            fold += seg[r];
                        }
                        l /= 2;
                        r /= 2;
                    }
                    fold
                };
                if k <= fold {
                    continue;
                }

                // add(k - 1, 1);
                {
                    let mut i = k - 1 + n;
                    seg[i] += 1;
                    i /= 2;
                    while 0 < i {
                        seg[i] = seg[2 * i] + seg[2 * i + 1];
                        i /= 2;
                    }
                }
                ans += d;
            }
        }

        println!("{}", ans);
    }
}
