#include "bits/stdc++.h"

using namespace std;

typedef long double ld;
typedef long long ll;

#define sz(x) (int)(x).size()
#define pb push_back
#define mp make_pair
#define f first
#define s second

template<typename T, typename U> bool ckmin(T &a, U b){ return b < a ? a = b, true : false; }
template<typename T, typename U> bool ckmax(T &a, U b){ return b > a ? a = b, true : false; }

struct camel{
    ll l, r, k;
    bool operator<(camel &oth){
            return (abs(l - r) > abs(oth.l - oth.r));
    }
};

ll tt, n;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> tt;
    while(tt--){
        cin >> n;
        ll ans = 0; camel tmp;
        vector<camel> v;
        for(int i = 0; i < n; ++i){
            cin >> tmp.k >> tmp.l >> tmp.r;
            tmp.k--; v.pb(tmp); 
        }
        sort(v.begin(), v.end());
        // for(auto i : v) cout << i.l - i.r << " " << i.k << "\n";
        // cout << "\n";
        set<int, greater<int>> un;
        set<int> run; 
        // things that can be pushed up and things that can be pushed down
        set<pair<ll, ll>> up;
        set<pair<ll, ll>, greater<pair<ll, ll>>> down;
        for(int i = 0; i < n; ++i){
            un.insert(i); run.insert(i);
        }
        for(int i = 0; i < n; ++i){
            ll l = v[i].l, r = v[i].r, k = v[i].k;
            if(l - r > 0){
                auto it = un.lower_bound(k);
                if(it == un.end()){
                    if(sz(down)){
                        auto it1 = down.begin();
                        if(it1->f > k) goto oth;
                        auto it = run.lower_bound(it1->f);
                        if(it != run.end()){
                            down.insert({*it, it1->s});
                            up.insert({it1->f, i});
                            un.erase(*it);
                            run.erase(*it);
                            down.erase(down.begin());
                            ans += l;
                            goto oth4;
                        }
                        else goto oth;
                    }
                    oth:;
                    ans += r;
                    oth4:;
                }
                else{
                    up.insert({*it, i});
                    run.erase(*it); 
                    un.erase(*it);
                    ans += l;
                }
            }
            else if(l - r == 0) ans += l;
            else{
                auto it = run.upper_bound(k);
                if(it == run.end()){
                    if(sz(up)){
                        // cout << "HERE" << " " << l << " " << r << "\n";
                        auto it1 = up.begin();
                        // cout << it1->f << " " << it1->s << "\n";
                        if(it1->f <= k) goto oth2;
                        auto it = un.lower_bound(it1->f);
                        // cout << *it << "\n";
                        // cout << ans << "\n";
                        if(it != un.end()){
                            up.insert({*it, it1->s});
                            down.insert({it1->f, i});
                            run.erase(*it);
                            un.erase(*it);
                            up.erase(up.begin());
                            ans += r;
                            goto oth3;
                        }
                        else goto oth2;
                    }
                    oth2:;
                    ans += l;
                    oth3:;
                }
                else{
                    down.insert({*it, i});
                    un.erase(*it);
                    run.erase(*it); 
                    ans += r;
                }
            }
        }
        cout << ans << "\n";
    }
    return 0;
}