#include<bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define pw(x) (1LL<<x)
#define eprintf(...) fprintf(stderr, __VA_ARGS__); fflush(stderr)

#define db(x) cerr << #x << ": " << x << '\n'
#define db2(x, y) cerr << "(" << #x << ", " << #y << "): (" << x << ", " << y << ")\n"
#define db3(x, y, z) cerr << "(" << #x << ", " << #y << ", " << #z << "): (" << x << ", " << y << ", " << z << ")\n"
#define dbv(a) cerr << #a << ": "; for (auto xxxx: a) cerr << xxxx << " "; cerr << '\n'

typedef long long ll;
typedef double dbl;
#define int ll

/*
ll slow(int n, vector<int> k, vector<ll> l, vector<ll> r) {
    ll res = 0;
    vector<int> p(n);
    iota(all(p), 0);
    do {
        ll cur = 0;
        for (int i = 0; i < n; ++i) {
            cur += p[i] <= k[i] ? l[i] : r[i];
        }
        res = max(res, cur);
    } while (next_permutation(all(p)));
    return res;
}
*/

ll solveA(int n, vector<int> k, vector<ll> a) {
    assert(sz(k) == sz(a));
    for (ll x: a) {
        assert(x >= 0);
    }
    for (int x: k) {
        assert(x >= 0 && x < n);
    }

    vector<vector<ll>> by_k(n);
    for (int i = 0; i < sz(k); ++i) {
        by_k[k[i]].pb(a[i]);
    }

    multiset<ll> cur;
    ll sum = 0;
    for (int i = 0; i < n; ++i) {
        for (ll x: by_k[i]) {
            sum += x;
            cur.insert(x);
        }

        while (sz(cur) > i+1) {
            ll x = *cur.begin();
            sum -= x;
            cur.erase(cur.begin());
        }
    }

    return sum;
}

ll solve(int n, vector<int> k, vector<ll> l, vector<ll> r) {
    ll res = 0;
    int cnt = 0;

    vector<int> k_l, k_r;
    vector<ll> a_l, a_r;
    for (int i = 0; i < n; ++i) {
        if (l[i] >= r[i]) {
            ++cnt;
        }
    }

    for (int i = 0; i < n; ++i) {
        if (l[i] >= r[i]) {
            res += r[i];
            k_l.pb(min(cnt-1, k[i]));
            a_l.pb(l[i]-r[i]);
        } else {
            res += l[i];
            if (k[i]+1 == n) {
                continue;
            }

            k_r.pb(n-max(cnt, k[i]+1)-1);
            a_r.pb(r[i]-l[i]);
        }
    }

    //db(solveA(cnt, k_l, a_l));
    //db(solveA(n-cnt, k_r, a_r));
    res += solveA(cnt, k_l, a_l);
    res += solveA(n-cnt, k_r, a_r);

    return res;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> k(n);
        vector<ll> l(n), r(n);
        for (int i = 0; i < n; ++i) {
            cin >> k[i] >> l[i] >> r[i];
            --k[i];
        }
        ll res = solve(n, k, l, r);
        cout << res << '\n';
    }
    /*
    int t;
    scanf("%d", &t);
    for (int test_id = 0; test_id < t; ++test_id) {
        srand(test_id);
        int n = rand() % 7 + 1;
        vector<int> k(n);
        vector<ll> l(n), r(n);
        for (int i = 0; i < n; ++i) {
            k[i] = rand() % n;
            l[i] = rand() % 20 + 1;
            r[i] = rand() % 20 + 1;
        }
        ll res_slow = slow(n, k, l, r);
        ll res_solve = solve(n, k, l, r);
        //ll res_slow01 = slow01(n, k, l, r);
        if (res_slow  != res_solve) {
            db(test_id);
            eprintf("res_slow: %I64d, res_solve: %I64d\n", res_slow, res_solve);
            db(n);
            dbv(k);
            dbv(l);
            dbv(r);
            return 0;
        }
    }*/
    return 0;
}


Measured memory_bytes: 26284