#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 2e5 + 5;
ll n, T, b[maxn], t = 0, m = 0 , k, a[maxn], ans, vit[maxn], f[maxn];
struct dang
{
    ll x, y, dd;
};

dang c[maxn];
bool check, ck;
string s;
vector<ll> adj[2][maxn];
inline void sol()
{
    cin >> n;
    ans = 0;

    for(int i = 1; i <= n; i ++)
    {
        ll k, l, r;
        cin >> k >> l  >> r;
        if(l >= r)
        {
            adj[0][k].pb(l - r);
        }
        else
        {
            adj[1][n-k].pb({r - l});
        }
        ans += min(l, r);
    }
    for(int v = 0; v < 2; v ++)
    {
        priority_queue< ll, vector<ll>, greater<ll> >pr;
        for(int i = 1; i <= n; i ++)
        {
            for(ll u : adj[v][i])
            {
                pr.push(u);
                ans += u;
            }
            while((ll)pr.size() > i)
            {
                ans -= pr.top();
                pr.pop();
            }
        }
    }

    cout << ans << '\n';
    for(int i = 1; i <= n; i ++)adj[0][i].clear(), adj[1][i].clear();
}
int main()
{
	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	cin >> T;
	while(T -- > 0)
	sol();
}
