#include<bits/stdc++.h>
using namespace std;

using ll=long long;

ll solve(){

    int n;
    cin >> n;
    vector<priority_queue<ll>> camel(n);
    ll idol=0;
    ll ans;
    for(int i=0;i<n;i++){
      ll k,l,r;
      cin >> k >> l >> r;k--;
      idol+=l;
      camel[k].push(l-r);//(r-l)=rに入れた場合のlに入ってた時との場合との差
    }

    //cout << "idol:" << idol << endl;
    ll index=-1;
    ll distance=0;
    priority_queue<ll> que;
    for(int i=0;i<n;i++){
      que=camel[i];
      //cout << "i:" << i << endl;
      //cout << "size:" << que.size() << endl;
      if(i==n-1) continue;
      while(1){
        if(index==i || que.empty()) break;
        if(que.top()<0) break;
        //cout << "que.top():" << que.top() << endl;
        que.pop();
        index++;
        //cout << "index:" << index << endl;
      }
      while(!que.empty()){
        distance+=que.top();
        que.pop();
      }
    }
    //cout << "distance:" << distance << endl;
    ans=idol-distance;
    return ans;
}

int main(){

  int t;
  cin >> t;
  //vector<ll> ans;
  for(int i=0;i<t;i++){
    cout << solve() << end;
    //ans.push_back(solve());
  }

  /*cout << "ans" << endl;
  for(int i=0;i<t;i++){
    cout << ans[i] << endl;
  }*/
}