#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i,n,k) for(int i=(n);i<(k);i++)
#define ALL(a)  (a).begin(),(a).end()
ll calc(vector<pair<ll,ll>> &vec, int n){
    ll ret=0;
    priority_queue<ll, vector<ll>, greater<ll>> que;
    auto itr = vec.begin();
    For(i,0,n+1){
        while(itr < vec.end() && (*itr).first <= i){
            que.push((*itr).second);
            ret += (*itr).second;
            itr++;
        }
        while(i<que.size()){
            ret -= que.top();
            que.pop();
        }
    }
    return ret;
}
ll solve(){
    int n;
    cin >> n;
    vector<pair<ll, ll>> left,right;
    ll sum = 0;
    For(i,0,n){
        ll k,l,r;
        cin >> k >> l >> r;
        if(l>r){
            sum += r;
            left.push_back({k,l-r});
        }
        else{
            sum += l;
            right.push_back({n-k,r-l});
        }
    }
    sort(ALL(left));
    sort(ALL(right));
    sum += calc(left,n) + calc(right,n);
    return sum;
}
void Main(){
    int t;
    cin >> t;
    vector<ll> ans(t);
    For(i,0,t) ans[i] = solve();
    For(i,0,t){
        cout << ans[i] << endl;
    }
}
int main(){
    Main();
    /*
    東方風神録は神が出てくるので当然神ゲー
    */
    return 0;
}