#include<iostream>
#include<algorithm>
#include<iterator>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<ll,int> P;
int main()
{
  int t,n,k,l,r,tt,i;
  ll res;
  vector<int> e;
  vector<P> p;
  decltype(e)::iterator it;
  cin >> t;
  for(tt=0;tt<t;tt++){
    cin >> n;
    res=0;
    e.clear();
    p.clear();
    for(i=0;i<n;i++){
      cin >> k >> l >> r;
      res+=r;
      p.emplace_back(l-r,k-1);
    }
    sort(p.begin(),p.end(),greater<P>());
    for(i=0;i<n;i++){
      if(p[i].first<=0)
        break;
      e.push_back(i);
    }
    for(i=0;i<n;i++){
      if(p[i].first<=0)
        break;
      it=upper_bound(e.begin(),e.end(),p[i].second);
      if(it==e.begin()){
        continue;
      }
      e.erase(prev(it));
      res+=p[i].first;
    }
    e.clear();
    for(i=n-1;i>=0;i--){
      if(p[i].first>=0)
        break;
      e.push_back(i);
    }
    for(i=n-1;i>=0;i--){
      if(p[i].first>=0)
        break;
      it=upper_bound(e.begin(),e.end(),p[i].second+1,greater<int>());
      if(it==e.begin()){
        res+=p[i].first;
        continue;
      }
      e.erase(prev(it));
    }
    cout << res << endl;
  }
  return 0;
}