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

#define rep(i, m, n) for(int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for(int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
// constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

struct Edge {
    ll to, rev;
    long double cap;
    Edge(ll _to, long double _cap, ll _rev) {
        to = _to;
        cap = _cap;
        rev = _rev;
    }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph &G, ll from, ll to, long double cap, bool revFlag,
              long double revCap) {
    G[from].push_back(Edge(to, cap, (ll)G[to].size()));
    if(revFlag)
        G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void solve() {
    ll n;
    cin >> n;
    vec<tuple<ll, ll, ll>> p, m;
    REP(i, n) {
        ll k, l, r;
        cin >> k >> l >> r;
        k--;
        if(l > r)
            p.push_back({-k, l, r});
        else
            m.push_back({k + 1, l, r});
    }
    sort(all(p));

    vec<ll> used(n, false);
    ll con = n;
    ll sum = 0;
    ll s = n - m.size();
    int pos = 0;
    priority_queue<tuple<ll, ll>> que;
    REP2(i, s) {
        while(pos < p.size()) {
            auto [kk, l, r] = p[pos];
            if(-kk < i)
                break;
            pos++;
            que.push({(l - r), l});
        }
        if(!que.size())
            continue;
        auto [dl, l] = que.top();
        que.pop();
        sum += l;
        //cout << sum << en;
    }
    while(que.size()) {
        auto [dl, l] = que.top();
        que.pop();
        sum += l - dl;
        //cout << sum << en;
    }

    sort(all(m));

    pos = 0;
    rep(i, s, n) {
        while(pos < m.size()) {
            auto [kk, l, r] = m[pos];
            if(kk > i)
                break;
            pos++;
            que.push({(r - l), r});
        }
        if(!que.size())
            continue;
        auto [dr, r] = que.top();
        que.pop();
        sum += r;
        //cout << sum << en;
    }

    while(pos < m.size()) {
        auto [kk, l, r] = m[pos];
        pos++;
        que.push({(r - l), r});
    }

    while(que.size()) {
        auto [dr, r] = que.top();
        que.pop();
        sum += r - dr;
        //cout << sum << en;
    }

    cout << sum << en;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll t;
    cin >> t;
    REP(i, t)
    solve();

    return 0;
}


Measured memory_bytes: 13076