fn main() {
    let t: usize = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        buf.trim_end().parse().unwrap()
    };

    for _ in 0..t {
        let n: usize = {
            let mut buf = String::new();
            std::io::stdin().read_line(&mut buf).unwrap();
            buf.trim_end().parse().unwrap()
        };
        let (mut l, mut r) = {
            (
                Vec::<isize>::with_capacity(n),
                Vec::<isize>::with_capacity(n),
            )
        };
        let (mut left, mut right) = (Vec::new(), Vec::new());
        for i in 0..n {
            let mut buf = String::new();
            std::io::stdin().read_line(&mut buf).unwrap();
            let mut iter = buf.split_whitespace();
            let k: usize = iter.next().unwrap().parse().unwrap();
            l.push(iter.next().unwrap().parse().unwrap());
            r.push(iter.next().unwrap().parse().unwrap());
            if l[i] > r[i] {
                left.push((k, i));
            } else {
                right.push((n - k, i));
            }
        }

        let mut ans = 0;
        {
            left.sort();
            let mut heap = std::collections::BinaryHeap::new();
            for (k, i) in left {
                if heap.len() < k {
                    ans += l[i];
                    heap.push((r[i] - l[i], i));
                } else {
                    let &(x, i) = heap.peek().unwrap();
                    if x > r[i] - l[i] {
                        ans += x;
                        heap.pop();
                        ans += l[i];
                        heap.push((r[i] - l[i], i));
                    } else {
                        ans += r[i];
                    }
                }
            }
        }
        {
            right.sort();
            let mut heap = std::collections::BinaryHeap::new();
            for (k, i) in right {
                if k == 0 {
                    ans += l[i];
                } else if heap.len() < k {
                    ans += r[i];
                    heap.push((l[i] - r[i], i));
                } else {
                    let &(x, i) = heap.peek().unwrap();
                    if x > l[i] - r[i] {
                        ans += x;
                        heap.pop();
                        ans += r[i];
                        heap.push((l[i] - r[i], i));
                    } else {
                        ans += l[i];
                    }
                }
            }
        }
        println!("{}", ans);
    }
}