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

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()
#define PI acos(-1)

// code starts
typedef pair<int,int> P;

void solve()
{
  int n;
  cin >> n;
  vector<int> k(n);
  vector<int> l(n);
  vector<int> r(n);
  int i,j;
  rep(i,n) cin >> k[i] >> l[i] >> r[i];
  rep(i,n) k[i]--;
  vector<vector<int>> vs(n);
  ll ans = 0;
  rep(i,n)
  {
    vs[k[i]].emplace_back(l[i]-r[i]);
  }
  priority_queue<int,vector<int>,greater<int>> pq;
  rep(i,n)
  {
    rep(j,vs[i].size())
    {
      if(vs[i][j] >= 0)
      {
        pq.push(vs[i][j]);
      }
    }
    while(pq.size() > i+1)
    {
      pq.pop();
    }
  }
  while(!pq.empty())
  {
    ans += pq.top();
    pq.pop();
  }
  for(i=n-1;i>=0;i--)
  {
    rep(j,vs[i].size())
    {
      if(vs[i][j] < 0)
      {
        pq.push(-vs[i][j]);
      }
    }
    while(pq.size() > n-i-1)
    {
      pq.pop();
    }
  }
  while(!pq.empty())
  {
    ans += pq.top();
    pq.pop();
  }
  rep(i,n)
  {
    ans += min(l[i],r[i]);
  }
  cout << ans << "\n";
}

int main()
{
  int t;
  cin >> t;
  int i;
  rep(i,t) solve();
}
