#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// mt19937 gen_rand_int(chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64 gen_rand_ll(chrono::steady_clock::now().time_since_epoch().count());
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

long long power(long long a, long long b,long long m) {
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a %m;
        a = a * a %m;
        b >>= 1;
    }
    return res;
}


signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);  
cout.tie(0);   

#ifndef ONLINE_JUDGE
    if(fopen("INPUT.txt","r"))
    {
    freopen ("INPUT.txt" , "r" , stdin);
    freopen ("OUTPUT.txt" , "w" , stdout);
    }
#endif  

    int t;
    cin>>t;
    while (t--)
    {
        int n;
        cin>>n;
        vector<pair<pair<int,int> ,pair<int,int> > >pp;
        int k,l,r;
        for(int i=0;i<n;i++)
        {
            cin>>k>>l>>r;
            pp.push_back({{abs(l-r),k},{l,r}});
        } 
        sort(pp.begin(),pp.end(),greater<>());
        set<int>s;
        for(int i=1;i<=n;i++)
            s.insert(i);
        int ans=0;
        for(auto i:pp)
        {
            if(i.second.first>i.second.second)
            {
                auto j=s.lower_bound(i.first.second+1);
                if(j==s.begin())
                {
                    ans+=i.second.second;
                    s.erase(j);
                }
                else
                {
                    j--;
                    s.erase(j);
                    ans+=i.second.first;
                }                
            }
        }
        int sz=s.size();
        s.clear();
        for(int i=n;i>n-sz;i--)
            s.insert(i);
        for(auto i:pp)
        {
            if(i.second.first<=i.second.second)
            {
                auto j=s.lower_bound(i.first.second+1);
                if(j==s.end())
                {
                    j=s.begin();
                    ans+=i.second.first;
                    s.erase(j);
                }
                else
                {
                    s.erase(j);
                    ans+=i.second.second;
                }                
            }
        }
        cout<<ans<<'\n';
    }
        

} 
    
