#include <bits/stdc++.h>
using namespace std;
#define SZ(a) ((int)(a).size())
#define PB push_back
#define MP make_pair
const int maxn = 200009;
const int MOD = 1e9 + 7;
int main(){
#ifdef LOCAL
  freopen("a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false),cout.tie(0),cin.tie(0);
  int T;cin>>T;
  while(T--){
    int n;
    int64_t res=0;
    cin>>n;
    vector<pair<int,int>> A,B;
    for(int i=1;i<=n;++i){
      int k,l,r;
      cin>>k>>l>>r;
      if(l<r)B.emplace_back(n-k,r-l);
      else A.emplace_back(k,l-r);
      res+=min(l,r);
    }
    auto cal=[&](vector<pair<int,int>> &C){
      sort(C.rbegin(),C.rend());
      priority_queue<int> Q;
      for(int i=n,j=0;i>=1;--i){
        while(j<SZ(C)&&C[j].first==i)Q.push(C[j++].second);
        if(SZ(Q)){
          res+=Q.top();
          Q.pop();
        }
      }
    };
    cal(A);
    cal(B);
    cout<<res<<'\n';
  }
  
  return 0;
}
//2020.07.13 13:06:12
