#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T> inline bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}

#ifdef DEBUG
template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
using arr = array<ll, 4>;

struct UnionFind {
    vector<int> par;
    vector<int> count;
    vector<int> rank;
    function<ll(ll, ll)> nk;

    UnionFind(int N, function<ll(ll,ll)> nkt) {
        count.assign(N, 1);
        rank.assign(N, 0);
        par.assign(N, 0);
        rep(i, N) par[i] = i;
        nk = nkt;
    }

    int root(int x) {
        if (par[x] == x)
            return x;
        else {
            par[x] = root(par[x]);
            return par[x];
        }
    }

    void unite(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return;
        par[y] = par[x] = nk(par[y], par[x]);
        return;
    }

    int size(int x) {
        return count[root(x)];
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }
};
using UF = struct UnionFind;

void solve(){
    ll N;
    cin >> N;
    vc<arr> A(N);
    rep(i, N){
        cin >> A[i][0] >> A[i][1] >> A[i][2];
        A[i][0]--;
        A[i][3] = abs(A[i][1] - A[i][2]);
    }

    auto comp = [&](arr a1, arr a2) { return a1[3] < a2[3]; };
    priority_queue<arr, vc<arr>, decltype(comp)> pq{comp};
    rep(i, N) pq.emplace(A[i]);

    ll ans = 0;
    UF ul(N + 1, [](ll a, ll b) { return (a < b ? a : b); }), ur(N + 1, [](ll a, ll b) { return (a > b ? a : b); });

    while(!pq.empty()){
        auto [k, l, r, d] = pq.top();
        pq.pop();
        if(l > r){
            if(ul.root(k + 1)==0){
                ur.unite(k + 1, ur.par[k + 1] + 1);
                ans += r;
            }
            else{
                ul.unite(k + 1, ul.par[k + 1] - 1);
                ans += l;
            }
        }
        else{
            if(ur.root(k + 1) == N){
                ul.unite(k + 1, ul.par[k + 1] - 1);
                ans += l;
            }
            else{
                ur.unite(k + 1, ur.par[k + 1] + 1);
                ans += r;
            }
        }
    }

    cout << ans << endl;
}
int main() {
    init();
    ll T;
    cin >> T;
    rep(_, T) solve();
}

Measured memory_bytes: 20412