#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define FI first
#define SE second
#define maxn 200000
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef double db;

int n;
int k[maxn+5],l[maxn+5],r[maxn+5];

ll solve(vector<pii> A)
{
    ll res=0;
    sort(A.begin(),A.end());
    int ptr=A.size()-1;
    priority_queue<int> q;
    per(i,1,n)
    {
        while(ptr>=0 && A[ptr].FI==i) q.push(A[ptr--].SE);
        if(q.size())
        {
            res+=q.top();
            q.pop();
        }
    }
    return res;
}

int main()
{
    int CAS; scanf("%d",&CAS);
    while(CAS--)
    {
        scanf("%d",&n);
        rep(i,1,n) scanf("%d%d%d",&k[i],&l[i],&r[i]);
        ll ans=0;
        rep(i,1,n) if(l[i]>=r[i]) ans+=r[i]; else ans+=l[i];
        vector<pii> A,B;
        rep(i,1,n) if(l[i]>=r[i]) A.pb({k[i],l[i]-r[i]}); else B.pb({n-k[i],r[i]-l[i]});
        ans+=solve(A);
        ans+=solve(B);
        printf("%lld\n",ans);
    }
    
    return 0;
}