#include<bits/stdc++.h>
#define ll long long
#define pii_ pair<int,int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define show1(a) cout<<#a<<" = "<<a<<endl
#define show2(a,b) cout<<#a<<" = "<<a<<"; "<<#b<<" = "<<b<<endl
using namespace std;
const ll INF = 1LL<<60;
const int inf = 1<<30;
const int maxn = 2e5+5;
inline void fastio() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}

int n;
vector<pii_> lft,rgt;
ll cal(vector<pii_> a)
{
    int n = a.size();
    ll res = 0;
    set<int> pos;
    rep(i,1,n) pos.insert(i);
    for(auto x:a){
        auto it = pos.upper_bound(x.se);
        if(it!=pos.begin()){
            it--;
            res += x.fi;
            pos.erase(it);
        }
    }
    return res;
}
int main()
{
    fastio();
    int _;
    for(cin>>_;_;_--){
        cin>>n; ll ans = 0;
        lft.clear(),rgt.clear();
        rep(i,1,n){
            int k,l,r; cin>>k>>l>>r;
            if(l>r) lft.pb({l-r,k}),ans+=r;
            else rgt.pb({r-l,n-k}),ans+=l;
        }
        sort(lft.begin(),lft.end());sort(rgt.begin(),rgt.end());
        reverse(lft.begin(),lft.end());reverse(rgt.begin(),rgt.end());
        ans = ans + cal(lft) + cal(rgt);
        cout<<ans<<endl;
    }
    return 0;
}
