#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
using ll = long long;
using P = pair<ll,ll>;

int main(){
  int t;
  cin >> t;
  vector<ll> ans(t);
  rep(x,t) {
    ll n, k, l, r;
    cin >> n;
    vector<P> plus, minus; 
    rep(i,n) {
      cin >> k >> l >> r;
      ans[x] += r;
      if(l-r > 0) plus.push_back(P(k,l-r));
      else if(l-r < 0 && k < n) {
        ans[x] += l - r;
        minus.push_back(P(n-k,r-l));
      }
      else ans[x] += l-r;
    }
    sort(plus.begin(),plus.end());
    sort(minus.begin(),minus.end());
    priority_queue<ll,vector<ll>,greater<ll>> que;
    for(P p : plus) {
      que.push(p.second);
      while(que.size() > p.first) {
        que.pop();
      }
    }
    while(que.size()) {
      ans[x] += que.top();
      que.pop();
    }
    for(P m : minus) {
      que.push(m.second);
      while(que.size() > m.first) {
        que.pop();
      }
    }
    while(que.size()) {
      ans[x] += que.top();
      que.pop();
    }
  }
  rep(i,t) cout << ans[i] << endl;
  return 0;
}
