import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range, core.bitop;

alias C = Tuple!(int, "i", int, "k", long, "p");

void solve()
{
    auto N = readln.chomp.to!int;
    long res;
    C[] fs1, fs2, bs1, bs2;
    foreach (i; 0..N) {
        auto klr = readln.split.to!(int[]);
        auto K = klr[0];
        auto L = klr[1];
        auto R = klr[2];
        auto c = C(i, K, 0);
        if (L >= R) {
            c.p = L - R;
            res += R;
            fs1 ~= c;
            fs2 ~= c;
        } else {
            c.p = R - L;
            res += L;
            bs1 ~= c;
            bs2 ~= c;
        }

    }
    auto memo = new bool[](N);
    sort!"a.k > b.k"(fs1);
    sort!"a.p == b.p ? a.k < b.k : a.p < b.p"(fs2);
    auto base = fs1.length;
    int cnt;
    foreach_reverse (i; 1..fs1.length+1) {
        while (!fs1.empty && fs1[0].k >= i) {
            if (!memo[fs1[0].i]) {
                res += fs1[0].p;
                ++cnt;
            }
            memo[fs1[0].i] = true;
            fs1 = fs1[1..$];
        }
        if (cnt > 0) {
            --cnt;
        } else {
            while (memo[fs2[0].i]) fs2 = fs2[1..$];
            memo[fs2[0].i] = true;
            fs2 = fs2[1..$];
        }
    }

    sort!"a.k < b.k"(bs1);
    sort!"a.p == b.p ? a.k > b.k : a.p < b.p"(bs2);
    foreach (i; base..N) {
        while (!bs1.empty && bs1[0].k <= i) {
            if (!memo[bs1[0].i]) {
                res += bs1[0].p;
                ++cnt;
            }
            memo[bs1[0].i] = true;
            bs1 = bs1[1..$];
        }
        if (cnt > 0) {
            --cnt;
        } else {
            while (memo[bs2[0].i]) bs2 = bs2[1..$];
            memo[bs2[0].i] = true;
            bs2 = bs2[1..$];
        }
    }
    writeln(res);
}

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_; 0..T) solve();
}