#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0;i<int(N);++i)
#define all(a) (a).begin(),(a).end()
typedef long long ll;

int N;
ll calc(vector<pair<int, ll>> &v){
    sort(all(v));
    int sz = v.size();
    ll res = 0;
    priority_queue<ll> pq;
    int idx = sz-1;
    for(int i = N - 1; i >= 0; i--){
        while(idx >= 0 && v[idx].first == i){
            pq.push(v[idx].second);
            idx--;
        }
        if(pq.empty())continue;
        res += pq.top();
        pq.pop();
    }
    return res;
}
void solve(){
    cin >> N;
    vector<pair<int, ll>> lefts, rights;
    ll base = 0;
    rep(i,N){
        int K;
        ll L, R;
        cin >> K >> L >> R;
        K--;
        ll minn = min(L, R);
        base += minn;
        L -= minn;
        R -= minn;
        if(L == R)continue;
        if(L > R)lefts.emplace_back(K, L);
        if(L < R){
            K++;
            rights.emplace_back(N-1-K, R);
        }
    }
    cout << base + calc(lefts) + calc(rights) << endl;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--)solve();
}
