#include <bits/stdc++.h>
using namespace std;
         
#define fast { ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define pb push_back
#define ll long long
#define ld long double
#define vll vector<ll>
#define infl LONG_LONG_MAX
#define infd LDBL_MAX
#define F first
#define S second
#define pll pair<ll,ll> 
#define G(a,b) get<a>(b)
#define ALL(v) v.begin(),v.end()
#define MP make_pair
#define MT make_tuple
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fi(i,a,b) for(ll i=(b-1);i>=a;i--)    
#define endl "\n"
#define nl cout<<"\n";
#define pr(x) cout<<x;
#define pr1(x) cout<<x<<" ";
#define pr2(x,y) cout<<x<<" "<<y;
#define sz(x) (ll)x.size() 
#define ara cout<<"ara-ara\n"; 
 

bool comp(tuple<ll,ll,ll>&x,tuple<ll,ll,ll>&y)
{
  return ((G(1,x)-G(2,x))>=(G(1,y)-G(2,y))); 
}

const ll N=1e6+6;
ll BIT[N],arr[N],n,q;

void update(ll idx,ll delta)
{
  while(idx<=n) 
  {
    BIT[idx]+=delta;
    idx+=(idx&(-idx));
  }
}

ll pref_sum(ll idx)
{
  ll res=0;
  while(idx>0) 
  {
     res+=BIT[idx];
     idx-=(idx&(-idx));
  }
  return res;
}

void func()
{
   ll n;
   cin>>n;
   
   tuple<ll,ll,ll>tp[n+1];
   f(i,1,n+1) 
   {
     cin>>G(0,tp[i])>>G(1,tp[i])>>G(2,tp[i]);
   }

   sort(tp+1,tp+n+1,comp);
   /*f(i,1,n+1)
   {
     cout<<G(0,tp[i])<<"  "<<G(1,tp[i])<<" "<<G(2,tp[i])<<endl;
   }*/

   bool mark[n+1]={};
   ll cnt=0,ans=0;
   vector<tuple<ll,ll,ll>>v;
   v.pb(MT(0,0,0));
   f(i,1,n+1)
   {
     if(G(1,tp[i])-G(2,tp[i])<=0) break;

     if(pref_sum(G(0,tp[i]))<G(0,tp[i]))
     {
       //ans+=G(1,tp[i])-G(2,tp[i]);
       cnt++;
       mark[i]=1;
       v.pb(tp[i]);
       update(G(0,tp[i]),1);
     } 
   }
   sort(ALL(v));
   /*for(auto i:v)
   {
    cout<<G(0,i)<<" "<<G(1,i)<<" "<<G(2,i)<<endl;
   }*/
   
   vector<tuple<ll,ll,ll>> x;
   f(i,1,n+1)
   {
    if(!mark[i]) v.pb(tp[i]);
   }

   sort(ALL(x));
   for(auto i:x) v.pb(i);
    
   /*for(auto i:v)
   {
    cout<<G(0,i)<<" "<<G(1,i)<<" "<<G(2,i)<<endl;
   }*/
   
   //cout<<ans<<endl;
   f(i,1,n+1)
   {
     if(i<=G(0,v[i])) 
     {
      //cout<<"bye "<<G(1,v[i])<<endl;
      ans+=G(1,v[i]);
     }
     else 
     {
      //if(i==3) cout<<"hi\n";
      ans+=G(2,v[i]);
     }
   }

   cout<<ans<<endl;
   f(i,1,n+1) BIT[i]=0;
}   
     
     
     
int main() 
{
    //fast
    //#ifndef ONLINE_JUDGE
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    //#endif // ONLINE_JUDGE
        
     
    ll ntc=1;
    cin>>ntc;
     
    f(i,1,ntc+1)
    {
      //cout<<"Case #"<<i<<": ";
       func();
    }
     
    return 0;
}