#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();
void solve() {
  int Q;
  cin >> Q;
  using Tup = tuple<ll, ll, ll>;
  vector<Tup> va, vb, vc;
  while (Q--) {
    ll N;
    cin >> N;
    va.clear(), vb.clear(), vc.clear();
    for (ll i = 0; i < N; i++) {
      ll k, l, r;
      cin >> k >> l >> r;
      --k;
      if (k == N - 1 || l == r) {
        vc.emplace_back(k, l, r);
      } else if (l > r) {
        va.emplace_back(k, l, r);
      } else {
        vb.emplace_back(k, l, r);
      }
    }
    sort(va.begin(), va.end());
    sort(vb.begin(), vb.end(), greater<Tup>());
    ll ans = 0;
    for (auto [k, l, r] : vc) {
      ans += l;
    }
    priority_queue<ll, vector<ll>, greater<ll>> pq1;
    ll cur = 0;
    for (auto [k, l, r] : va) {
      if (cur <= k) {
        ans += l;
        pq1.emplace(l - r);
      } else {
        ll val = pq1.top();
        if (val < l - r) {
          pq1.pop();
          ans -= val;
          ans += l;
          pq1.emplace(l - r);
        } else {
          ans += r;
        }
      }
    }
    priority_queue<ll, vector<ll>, greater<ll>> pq2;
    cur = N - 1;
    for (auto [k, l, r] : vb) {
      if (cur > k) {
        ans += r;
        pq2.emplace(r - l);
      } else {
        ll val = pq2.top();
        if (val < r - l) {
          pq2.pop();
          ans -= val;
          ans += r;
          pq2.emplace(r - l);
        } else {
          ans += l;
        }
      }
    }
    cout << ans << "\n";
  }
}
int main() {
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
  solve();
  cout << flush;
  return 0;
}
