//Author - Rahil Malhotra
#include <bits/stdc++.h>
using namespace std;

template <typename T> void debug(T t) { cout<<t<<endl; }
template<typename T, typename... Args> void debug(T t, Args... args) { cout<<t<<" "; debug(args...); }

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define int long long
#define double long double

#define type pair<int,pair<int,int>>
int n;
vector<type> fp,lp;
bool comp(type &num1,type &num2)
{
    int val1=num1.second.first-num1.second.second;
    int val2=num2.second.first-num2.second.second;
    if(val1>val2)
        return 1;
    return 0;
}
int func(int kval)
{
    set<int>s;
    int i=0,j=0;
    int ans=0;
    int marked[n+2]={},marked2[n+2]={};
    int taken=0;
    for(int i=0;i<fp.size();i++)
    {
        if(fp[i].first<=kval)
        {
            if(taken<kval)
            {
                taken++;
                ans+=fp[i].second.first;
            }
            else
                marked[i]=1;
        }
        else
            marked[i]=1;
    }
    for(int i=0;i<fp.size();i++)
    {
        if(marked[i]==1)
            if(taken<fp[i].first)
                ans+=fp[i].second.first;
            else
                ans+=fp[i].second.second;
        taken++;
    }
    set<int>st;
    for(int i=1;i<=n;i++)
        st.insert(i);
    for(int i=0;i<lp.size();i++)
    {
        int posi=lp[i].first;
        if(posi+1>taken)
        {
            if(posi+1>n)
                marked2[i]=1,ans+=lp[i].second.second;
            else if(st.find(posi+1)!=st.end())
            {
                st.erase(posi+1);
                ans+=lp[i].second.first;
            }
            else if(st.upper_bound(posi+1)==st.end())
                marked2[i]=1,ans+=lp[i].second.second;
            else
            {
                int get=(*st.upper_bound(posi));
                st.erase(get);
                ans+=lp[i].second.first;
            }
        }
    }

    return ans;
}
int32_t main()
{
    IOS;
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            int a,b,c;
            cin>>c>>a>>b;
            if(a>=b)
                fp.push_back({c,{a,b}});
            else
                lp.push_back({c,{b,a}});
        }
        sort(fp.begin(),fp.end(),comp);
        sort(lp.begin(),lp.end(),comp);
        //debug(func(0),func(1),func(2),func(3));
        int low=0;
        int high=n;
        while(low+2<high)
        {
            int mid1=(2*low+high)/3;
            int mid2=(low+2*high)/3;
            if(func(mid1)<func(mid2))
                low=mid1+1;
            else
                high=mid2-1;
        }
        int ans=0;
        for(int i=max(0ll,low-10);i<=min(n,low+10);i++)
            ans=max(ans,func(i));
        cout<<ans<<endl;
        lp.clear();
        fp.clear();
    }
}