#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#define k first
#define w second
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int N=200010;
pii a[N],b[N];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,cnta=0,cntb=0;
        cin>>n;
        ll res=0;
        for(int i=0;i<n;i++)
        {
            int k,l,r;
            cin>>k>>l>>r;
            if((k==0&&l>=r)||(k==n&&r>l)) 
            {
                res+=min(l,r);
                continue;
            }
            if(l>=r) res+=r,a[cnta++]={k,l-r};
            else res+=l,b[cntb++]={n-k,r-l};
        }
        sort(a,a+cnta),sort(b,b+cntb);
        priority_queue<int,vector<int>,greater<int> >heap;
        for(int i=0;i<cnta;i++)
        {
            heap.push(a[i].w);
            while(heap.size()>a[i].k) heap.pop();
        }
        while(heap.size())
        {
            res+=heap.top();
            heap.pop();
        }
        for(int i=0;i<cntb;i++)
        {
            heap.push(b[i].w);
            while(heap.size()>b[i].k) heap.pop();
        }
        while(heap.size())
        {
            res+=heap.top();
            heap.pop();
        }
        cout<<res<<endl;
    }
    return 0;
}