#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<db, db> pd;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

typedef set<int> si;
typedef set<ll> sl;
typedef map<int, int> mii;

#define mp make_pair
#define f first
#define s second
#define sz(x) (int)x.size()
#define len(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
//#define pf push_front
//#define pb push_back
#define eb emplace_back
#define ep emplace
#define lb lower_bound
#define ub upper_bound

#define vfor(i, a, b) for (int i = (a); i < (b); ++i)
#define f0r(i, a) vfor(i,0,a)
#define rof(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define r0f(i, a) rof(i,0,a)
#define trav(a, x) for (auto& a: x)
#define def(fname, rtype, args, ...) function<rtype args> fname = [__VA_ARGS__] args

#define boost() cin.tie(0),cin.sync_with_stdio(0)

template<class T>
void binstf(T &found, T a, T b, function<bool(T)> test, bool TF = true) {
    T i = a, j = b;
    bool da = test(a), db = test(b);
    if ((da != TF) or (db == TF)) {
        cerr << "---[BS: TF = " << TF << "]---" << endl;
        cerr << a << "->" << da << endl;
        cerr << b << "->" << db << endl;
    }
    while (j - i > 1) {
        T m = (i + j) / 2;
        if (test(m) == TF)
            i = m;
        else
            j = m;
    }
    found = i;
    if (not TF)
        found++;
}

#define binsft(a, b, c, d) binstf(a,b,c,d,false);

template<class T>
T dpow(T a, T power) {
    T result = 1;
    while (power) {
        if (power % 2)result *= a;
        power /= 2, a = a * a;
    }
    return result;
}

const ld PI = acos((ld) -1);
mt19937 rng((uint32_t) chrono::steady_clock::now().time_since_epoch().count());

template<class T>
bool ckmin(T &a, const T &b) {
    return b < a ? a = b, 1 : 0;
}

template<class T>
bool ckmax(T &a, const T &b) {
    return a < b ? a = b, 1 : 0;
}

template<class T>
bool reorder(T &a, T &b) {
    return (a > b) ? swap(a, b), 1 : 0;
}

// INPUT
template<class A>
void re(complex<A> &c);

template<class A, class B>
void re(pair<A, B> &p);

template<class A>
void re(vector<A> &v);

template<class A, size_t SZ>
void re(array<A, SZ> &a);

#define ore(type, x) type x;re(x);
#define ire(x) ore(int,x);
#define vire(x) ore(vi,x);
#define lre(x) ore(ll,x);

template<class T>
void re(T &x) { cin >> x; }

void re(db &d) {
    str t;
    re(t);
    d = stod(t);
}

void re(ld &d) {
    str t;
    re(t);
    d = stold(t);
}

template<class H, class... T>
void re(H &h, T &... t) {
    re(h);
    re(t...);
}

template<class A>
void re(complex<A> &c) {
    A a, b;
    re(a, b);
    c = {a, b};
}

template<class A, class B>
void re(pair<A, B> &p) { re(p.f, p.s); }

template<class A>
void re(vector<A> &x) { trav(a, x) re(a); }

template<class A, size_t SZ>
void re(array<A, SZ> &x) { trav(a, x) re(a); }

template<class T>
T gcd(T a, T b) {
    a = a < 0 ? -a : a;
    b = b < 0 ? -b : b;
    if (a * b == 0)
        return max(a, b);
    else return gcd(min(a, b), max(a, b) % min(a, b));
}


const int MODE107 = 1000000007;

template<class T>
T modop(T a, T mode = MODE107) {
    a = a % mode;
    if (a < 0)a += mode;
    return a;
}

template<class T>
T inv(T a, T mode = MODE107) {
    T coefa = 1, coefb = 0;
    T aa = a, bb = mode;
    while (aa != 1) {
        if (aa > bb)swap(coefa, coefb), swap(aa, bb);
        T cc = bb / aa;
        bb -= cc * aa, coefb -= cc * coefa;
        swap(coefa, coefb), swap(aa, bb);
    }
    return coefa;
}


const int uplrDX[] = {-1, 1, 0, 0};
const int uplrDY[] = {0, 0, -1, 1};

#define ts to_string

str ts(char c) { return str(1, c); }

str ts(bool b) { return b ? "true" : "false"; }

str ts(const char *s) { return (str) s; }

str ts(str s) { return s; }

template<class A>
str ts(complex<A> c) {
    stringstream ss;
    ss << c;
    return ss.str();
}

str ts(vector<bool> v) {
    str res = "{";
    f0r(i, sz(v)) res += char('0' + v[i]);
    res += "}";
    return res;
}

template<size_t SZ>
str ts(bitset<SZ> b) {
    str res = "";
    f0r(i, SZ) res += char('0' + b[i]);
    return res;
}

template<class A, class B>
str ts(pair<A, B> p);

template<class T>
str ts(T v) { // containers with begin(), end()
    bool fst = 1;
    str res = "{";
    for (const auto &x: v) {
        if (!fst) res += ", ";
        fst = 0;
        res += ts(x);
    }
    res += "}";
    return res;
}

template<class A, class B>
str ts(pair<A, B> p) {
    return "(" + ts(p.f) + ", " + ts(p.s) + ")";
}

// OUTPUT
template<class A>
void pr(A x) { cout << ts(x); }

template<class H, class... T>
void pr(const H &h, const T &... t) {
    pr(h);
    pr(t...);
}

void ps() { pr("\n"); } // print w/ spaces
template<class H, class... T>
void ps(const H &h, const T &... t) {
    pr(h);
    if (sizeof...(t)) pr(" ");
    ps(t...);
}

// DEBUG
void DBG() { cerr << "]" << endl; }

template<class H, class... T>
void DBG(H h, T... t) {
    cerr << ts(h);
    if (sizeof...(t)) cerr << ", ";
    DBG(t...);
}

#ifdef LOCAL // compile with -DLOCAL
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif


// REINITIALIZE ARRAYS BETWEEN TCs

/**
 * Description: Use in place of \texttt{complex<T>}.
 * Source: http://codeforces.com/blog/entry/22175, KACTL
 * Verification: various
 */

typedef int T;

int sgn(T a) { return (a > 0) - (a < 0); }

T sq(T a) { return a * a; }

namespace Point {
    typedef pair<T, T> P;
    typedef vector<P> vP;

    T norm(const P &p) { return sq(p.f) + sq(p.s); }

    T abs(const P &p) { return sqrt(norm(p)); }

    T arg(const P &p) { return atan2(p.s, p.f); }

    P conj(const P &p) { return P(p.f, -p.s); }

    P perp(const P &p) { return P(-p.s, p.f); }

    P dir(T ang) { return P(cos(ang), sin(ang)); }

    P operator-(const P &l) { return P(-l.f, -l.s); }

    P operator+(const P &l, const P &r) {
        return P(l.f + r.f, l.s + r.s);
    }

    P operator-(const P &l, const P &r) {
        return P(l.f - r.f, l.s - r.s);
    }

    P operator*(const P &l, const T &r) {
        return P(l.f * r, l.s * r);
    }

    P operator*(const T &l, const P &r) { return r * l; }

    P operator/(const P &l, const T &r) {
        return P(l.f / r, l.s / r);
    }

    P operator*(const P &l, const P &r) {
        return P(l.f * r.f - l.s * r.s, l.s * r.f + l.f * r.s);
    }

    P operator/(const P &l, const P &r) {
        return l * conj(r) / norm(r);
    }

    P &operator+=(P &l, const P &r) { return l = l + r; }

    P &operator-=(P &l, const P &r) { return l = l - r; }

    P &operator*=(P &l, const T &r) { return l = l * r; }

    P &operator/=(P &l, const T &r) { return l = l / r; }

    P &operator*=(P &l, const P &r) { return l = l * r; }

    P &operator/=(P &l, const P &r) { return l = l / r; }

    P unit(const P &p) { return p / abs(p); }

    T dot(const P &a, const P &b) { return a.f * b.f + a.s * b.s; }

    T cross(const P &a, const P &b) { return a.f * b.s - a.s * b.f; }

    T cross(const P &p, const P &a, const P &b) {
        return cross(a - p, b - p);
    }

    P reflect(const P &p, const P &a, const P &b) {
        return a + conj((p - a) / (b - a)) * (b - a);
    }

    P foot(const P &p, const P &a, const P &b) {
        return (p + reflect(p, a, b)) / (T) 2;
    }

    bool onSeg(const P &p, const P &a, const P &b) {
        return cross(a, b, p) == 0 && dot(p - a, p - b) <= 0;
    }

    ostream &operator<<(ostream &os, const P &p) {
        return os << "(" << p.f << "," << p.s << ")";
    }
};

//using namespace Point;

typedef unsigned long long ull;

class trie {
public:
    map<char, trie> l;

    void add(string t) {
        if (t.length() > 0) {
            l[t[0]].add(t.substr(1));
        }
    }

    bool contains(string t) {
        if (not l.count(t[0]))
            return false;
        return l[t[0]].contains(t.substr(1));
    }
};

template<class T>
class rq {
    /* range query
     */
    vector<vector<T>> content;
    function<T(T, T)> op;

public:
    rq(vector<T> &orig, function<T(T, T)> operation) {
        op = operation;
        int size = 1;
        content.emplace_back(orig);
        while (size < orig.size()) {
            size *= 2;
            content.emplace_back(0);
            auto &todo = content.back(), &from = content[content.size() - 2];
            f0r(i, sz(from) - size / 2) {
                todo.eb(op(from[i], from[i + size / 2]));//[i ~ i+s[ = [i, i+s/2[ + [i+s/2,i+s[
            }
        }
    }

    T query(int i, int j) {//[i,j[
        if (j <= i or j > content[0].size() or i < 0 or i >= content[0].size()) {
            cerr << "i=" << i << endl;
            cerr << "j=" << j << endl;
            throw "bad i j";
        }
        tie(i, j) = mp(min(i, j), max(i, j));
        int size = 1, pow = 0;
        while (size * 2 < j - i) {
            size *= 2, pow++;
        }
        assert(pow < content.size());
        auto &c = content[pow];
        assert(i < c.size() and i >= 0);
        assert(j - size < c.size() and j - size >= 0);
        T dbg = op(content[pow][i], content[pow][j - size]);
        return dbg;
    }
};

template<class T>
class maxflow {
public:
    typedef T unit;
    int src, drn;//source and drain node id
    vector<vi> adj, radj;
    map<pi, unit> lims;
    map<pi, unit> rlims;//will contain solution after solve()
    void genradj() {
        radj.resize(sz(adj));
        f0r(from, sz(adj)) {
            trav(to, adj[from]) {
                radj[to].emplace_back(from);
            }
        }
    }

    unit solve() {
        while (true) {
            map<int, pair<pi, unit>> backtrack;
            bool found = false;
            set<int> seen;//bfs from src
            queue<pair<int, unit>> todo;
            todo.emplace(src, -1);
            while (not todo.empty()) {
                int from = todo.front().f;
                unit bottleneck = todo.front().s;
                todo.pop();
                if (from == drn) {
                    found = true;
                    break;
                }
                trav(to, adj[from]) {
                    pi edge = mp(from, to);
                    unit bn = min(bottleneck, lims[edge]);
                    if (bn and not seen.count(to)) {
                        seen.emplace(to);
                        todo.emplace(to, bn);
                        backtrack[to] = mp(edge, bn);
                    }
                }

                trav(to, radj[from]) {
                    pi edge = mp(to, from);
                    unit bn = min(bottleneck, rlims[edge]);
                    if (bn and not seen.count(to)) {
                        seen.emplace(to);
                        todo.emplace(to, bn);
                        backtrack[to] = mp(edge, bn);
                    }
                }
            }
            if (not found)
                break;
            else {
                list<pi> edges;
                int to = drn;
                unit flow = backtrack[to].second;
                while (to != src) {
                    pi edge = backtrack[to].f;
                    int from = edge.f + edge.s - to;
                    edges.emplace_front(edge);
                    to = from;
                }
                int from = src;
                trav(e, edges) {
                    if (e.f == from)
                        rlims[e] += flow, lims[e] -= flow;
                    else
                        rlims[e] -= flow, lims[e] += flow;
                    from = e.f + e.s - from;
                }
            }
        }
        unit res = 0;
        trav(to, adj[src])res += rlims[mp(src, to)];
        return res;
    }

};

auto __tic__ = chrono::high_resolution_clock::now();

void tic() {
    __tic__ = chrono::high_resolution_clock::now();
}

void toc() {
    ps(chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - __tic__).count(), "us");
    cout << endl;
}


void precalc() {
    //start here
}

void solve(int ti) {
    //start here
    int n;
    re(n);
    vpi front, back;
    ull base = 0;

    long long ans = 0;//test
    vector<pair<int, int>> right, left;//test
    f0r(i, n) {
        int k, l, r;
        re(k, l, r);


        ans += min(l, r);//test
        if (l < r) {
            right.emplace_back(r - l, n - k);
        } else if (l > r) {
            left.emplace_back(l - r, k);
        }

        base += (k == n) ? l : min(l, r);
        if (l == r or k == n)
            continue;
        if (l > r)
            front.emplace_back(max(l, r) - min(l, r), k);
        if (l < r)
            back.emplace_back(max(l, r) - min(l, r), n - k);
    }
    def(sortbyrange, bool, (pi a, pi b), &) {
        if (a.second < b.second)return true;
        if (a.second == b.second)return a.first < b.first;
        return false;
    };
//    auto[a, b]=mp(1, 2.0);
    sort(all(front), sortbyrange);
    sort(all(back), sortbyrange);
    for (auto addr:{&front, &back}) {
        set<pi> heap;
        auto &front_or_back = *addr;
        f0r(i, front_or_back.size()) {
            int freespace = front_or_back[i].second;
            heap.emplace(front_or_back[i]);
            if (heap.size() > freespace)
                heap.erase(*heap.begin());
        }
        trav(e, heap)base += e.first;
    }
//    ps(base);
    //greedily take the bonus
    {
        for (int i = 0; i < n; ++i) {
        }
        sort(right.rbegin(), right.rend());
        sort(left.rbegin(), left.rend());

        auto proc = [&ans, &n](vector<pair<int, int>> left) {
            set<int> ndx;
            for (int i = 1; i <= n; ++i) {
                ndx.insert(i);
            }
            for (auto xk : left) {
                int x = xk.first, k = xk.second;
                auto it = ndx.upper_bound(k);
                if (it != ndx.begin()) {
                    ndx.erase(--it);
                    ans += x;
                }
            }
        };

        proc(left);
        proc(right);
        cout << ans << "\n";
    }
}

int main() {
    precalc();
    bool online = true;
#ifdef OFFLINE
    online = false;
    if (online)
        boost();
    string testinputs[] = {
            R"(
1
18
10 9999 8888
18 1 449
18 54 23
17 22 1000000000
5 1000000000 8
1 2 0
1 3 0
2 7 0
2 4 0
2 3 0
5 3 0
5 9 0
5 2 0
5 1 0
8 0 1
8 0 3
8 0 7
8 0 5

)",
//            R"(3 2
//1 1 0
//1 2
//1 3
//
//)",
    };//TODO
    trav(testinput, testinputs) {
        istringstream iss(testinput);
        cout << "---local mode---" << endl;
        cin.rdbuf(iss.rdbuf());
#endif
        // setIO("A");
        int T = 1;
        {
            re(T);
        }
        vfor(i, 1, T + 1) {
//        cout << "Case #" << i << ": ";//Google Code jam
            solve(i);
        }

#ifdef OFFLINE
    }
#endif
}

Measured memory_bytes: 16584