#include<bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

typedef long long ll;
typedef pair<int, int> P;

#define MAX 100005
#define INF 1001001001

int main(int, char**)
{
  int t;
  cin >> t;

  rep(_,t) {
    int n;
    cin >> n;

    vector< priority_queue<P> > diff(2);
    ll ans = 0;
    rep(i,n) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l >= r) {
        diff[0].push({k-1,l-r});
        ans += r;
      } else {
        diff[1].push({n-k-1,r-l});
        ans += l;
      }
    }

    rep(i,2) {
      int ds = diff[i].size();
      priority_queue<int> q;
      rep(j,ds) {
        while(!diff[i].empty() && diff[i].top().first >= ds-j-1) {
          q.push(diff[i].top().second); diff[i].pop();
        }
        if (!q.empty()) { ans += q.top(); q.pop(); }
      }
    }

    cout << ans << endl;
  }
  return 0;
}
