#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

ll calc(const vector<pair<int,int>>& v){
  ll ret = 0;

  priority_queue<int> que;
  int j = 0;
  for(int i=v.size()-1; i>=0; i--){
    while(j<v.size() && v[j].first >= i){
      que.push(v[j].second);
      j++;
    }

    if(!que.empty()){
      ret += que.top();
      que.pop();
    }
  }

  return ret;
}

void solve(){
  int N;
  cin >> N;
  vector<pair<int,int>> lhs, rhs;
  ll sum = 0;
  rep(i,N){
    int k, l, r;
    cin >> k >> l >> r;
    k--;
    int mn = min(l, r);
    sum += mn;
    l -= mn;
    r -= mn;

    if(l > 0){
      lhs.emplace_back(k,l);
    }else{
      rhs.emplace_back(N-1-(k+1),r);
    }
  }

  sort(ALLOF(lhs));
  reverse(ALLOF(lhs));
  sort(ALLOF(rhs));
  reverse(ALLOF(rhs));

  sum += calc(lhs);
  sum += calc(rhs);

  cout << sum << endl;
}


int main(){
  int T;
  cin >> T;
  rep(t,T){
    solve();
  }
  
  return 0;
}

