#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long int
// #define ll long long int
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define endl '\n'
#define all(x) x.begin(),x.end()
#define f(i,j,l) for(i=j;i<l;i++)
#define smax(a,b) a=max(a,b)
// #if !defined(ONLINE_JUDGE)
// #include "debugging.h"
// #endif
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
// erase,insert same as normal set

// int const mod = 7340033;
// int const mod = 998244353;
int const mod = 1e9+9;
int power(int a,int b,int m){
    int res = 1;
    while(b){
        if(b&1){
            // res = res*a;
            res = (res*a)%m;
        }
        a=(a*a)%m;
        // a%=m;
        b=b/2;
    }
    return res;
}
// ll mul(ll num1,ll num2,ll m){
//     ll x = (num1*num2)%m;
//     return x;
// }

// ll add(ll num1,ll num2,ll m){
//     ll x = ((num1%m)+(num2%m))%m;
//     return x;
// }

// ll sub(ll num1,ll num2,ll m){
//     ll x = (((num1%m)-(num2%m))%m + m)%m;
//     return x;
// }


int inverse(int x , int m){
    return power(x,m-2,m);
}

int const N = 1e5+5;
int const inf = 2e18;

void solve(){  
    int i,j;
    int n;
    cin >> n;
    vector<pair<pii, pii> > v;
    for(i=0;i<n;i++){
        int k,l,r;
        cin>>k>>l>>r;
        v.pb({{l-r,-1*k},{l,r}});
    }
    sort(all(v));
    set<int> st;
    for(i=1;i<=n;i++){
        st.insert(-i);
    }
    int ans = 0;
    for(i=0;i<n;i++){
        if(v[i].first.first >= 0){
            int k1 = v[i].first.second;
            // k1 = -k1;
            auto it = st.lower_bound(k1);
            if(it != st.end()){
                int x = *it;
                st.erase(st.find(x));
                ans = ans + v[i].second.first;
            }
            else{
                int x = *st.begin();
                st.erase(st.find(x));
                ans = ans + v[i].second.second;
            }
        }
        else{
            int x = *st.begin();
            st.erase(st.find(x));
            x = x*(-1);

            int y = v[i].first.second;
            y = y*(-1);
            if(y < x){
                ans = ans + v[i].second.second;
            }
            else{
                ans = ans + v[i].second.first;
            }
        }
    }
    cout<<ans<<endl;
}           

int32_t main(){
    IOS;
    int t;
    t = 1;
    // precompute();
    cin>>t;
    for(int tt=1;tt<=t;tt++){
        solve();
    }
    return 0;
}


/*



*/