Scanf.scanf "%d" (fun t ->
    let offset = 10000000 in
    for i = 1 to t do
        Scanf.scanf " %d" (fun n ->
            let klr = Array.init n (fun _ -> Scanf.scanf " %d %d %d" (fun k l r ->
                if l > r then k, r - l, l else offset + k, l - r, r)
                )
            in
            Array.sort compare klr;
            let rec loop i acc =
                if i = n then Printf.printf "%d\n" acc else
                    let k, sum, v = klr.(i) in
                    let acc = acc +
                        if k < offset then (
                            if i + 1 <= k then v else sum + v
                        ) else (
                            if i + 1 <= k - offset then sum + v else v
                        )
                    in
                    loop (i + 1) acc
            in
            loop 0 0
        )
    done
)