#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9+7;
const long long LINF = 1e18;
#define dump(x)  cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define FOREACH(x,a) for(auto& (x) : (a) )
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template<typename T>
void print(const vector<T> &x) {
    int n = x.size();
    rep(i,n) {
        cout << x[i];
        if (i!=n-1) cout<<" ";
        else cout << endl;
    }
}

template<typename T>
void print(const vector<vector<T>> &x) {
    int n = x.size();
    rep(i,n) {
        rep(j,x[i].size()) {
            cout << x[i][j] << " ";
        }
        cout << endl;
    }
}

template<typename T>
void print(const vector<T> &x, int n) {
    rep(i,n) {
        cout << x[i];
        if (i!=n-1) cout<<" ";
        else cout << endl;
    }
}

template<typename T>
void print(const vector<vector<T>> &x, int n, int m) {
    rep(i,n) {
        rep(j,m) {
            cout << x[i][j] << " ";
        }
        cout << endl;
    }
}

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void input_init() {
    cin.tie(0); ios::sync_with_stdio(false);
}

typedef tuple<ll,ll,ll> T;
int main(int argc, char const *argv[]) {
    int t; cin>>t;
    while (t--) {
        int n; cin>>n;
        vector<T> a;
        rep(i,n) {
            ll k,l,r;
            cin>>k>>l>>r;
            a.emplace_back(l,r,k);
        }
        sort(a.begin(), a.end(), greater<>());
        ll ans = 0;
        for (int i = 0; i < n; ++i) {
            ll l,r,k;
            tie(l,r,k) = a[i];
            if (i+1 <= k) ans+=l;
            else ans+=r;
        }
        //cout << ans << '\n';
        ll tmp = 0;
        sort(a.begin(), a.end(), [](const T L, const T R) {
            return get<1>(L)+get<0>(R) > get<1>(R)+get<0>(L);
        });
        for (int i = 0; i < n; ++i) {
            ll l,r,k;
            tie(l,r,k) = a[i];
            if (i+1 <= k) tmp+=l;
            else tmp+=r;
        }
        cout << tmp << '\n';
    }

    return 0;
}