#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
struct edge {
  ll to;
  ll cost;
};
using graph = vector<vector<edge>>;
const ll INF = 1e18;
inline void chmax(ll& a, ll b) { a = max(a, b); }
inline void chmin(ll& a, ll b) { a = min(a, b); }
double pi = acos(-1);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
  ll t;
  cin >> t;
  for (ll ww = 0; ww < (t); ++ww) {
    ll n;
    cin >> n;
    vector<ll> K(n), L(n), R(n), sa(n);
    ll sum = 0;
    ll ans = 0;
    for (ll i = 0; i < (n); ++i) {
      cin >> K[i] >> L[i] >> R[i];
      sa[i] = L[i] - R[i];
      sum += R[i];
    }
    map<ll, vector<ll>> mp;
    for (ll i = 0; i < (n); ++i) {
      mp[K[i]].push_back(sa[i]);
    }
    priority_queue<int> pq;
    for (ll i = n; i >= 1; i--) {
      for (auto u : mp[i]) {
        pq.push(u);
      }
      if (!pq.empty()) {
        ll x = pq.top();
        if (x > 0) {
          ans += x;
          pq.pop();
        }
      }
    }
    cout << ans + sum << endl;
  }
  return 0;
}
