#include <bits/stdc++.h>
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
#define e_b emplace_back
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
using namespace std;
using ll = long long;
using ld = long double;
template<class T> bool chmax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<class T> bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;}

bool calc(vector<int> &AA,int p){
  if(p==-1) return false;
  if(AA.at(p)==p){
    if(p==0)  AA.at(p)=-1;
    else  AA.at(p)=AA.at(p-1);
    return true;
  }
  if(calc(AA,AA.at(p))) return true;
  else{
    AA.at(p)=-1;
    return false;
  }
}

int main(){
  ios_base::sync_with_stdio(false);cin.tie(0);
  int T;
  cin >> T;
  for(int _=0;_<T;_++){
    int N;
    cin >> N;
    vector<pair<ll,int>> XX;
    vector<pair<ll,int>> YY;
    ll ans=0;
    for(int i=0;i<N;i++){
      int K;
      ll L,R;
      cin >> K >> L >> R;
      K--;

      ans+=R;
      ll tmp=L-R;
      if(tmp>=0)  XX.p_b(m_p(tmp,K));
      else  YY.p_b(m_p(tmp,N-K-1));
    }
    sort(all(XX));
    reverse(all(XX));
    sort(all(YY));
    reverse(all(YY));

    sizeX=sz(XX);
    vector<int> AA(sizeX);
    for(int i=0;i<sizeX;i++)  AA.at(i)=i;
    for(int i=0;i<sizeX;i++){
      if(calc(AA,XX.at(i).se))  ans+=XX.at(i).fi;
    }
    sizeY=sz(YY);
    vector<int> BB(sizeY);
    for(int i=0;i<sizeY;i++)  BB.at(i)=i;
    for(int i=0;i<sizeY;i++){
      if(calc(BB,YY.at(i).se))  ans+=YY.at(i).fi;
    }
    cout << ans << endl;
  }

  return 0;
}