#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
int main() {
  ll T;
  cin >> T;
  for (ll cs = 0; cs < T; cs++) {
    ll n;
    cin >> n;
    vector<tuple<ll, ll, ll, ll>> v(n);
    for (ll i = 0; i < n; i++) {
      ll k, l, r;
      cin >> k >> l >> r;
      v[i] = {l - r, k, l, r};
    }
    sort(v.rbegin(), v.rend());
    set<ll> st;
    for (ll i = 0; i < n; i++) {
      st.insert(i);
    }
    ll ans = 0;
    for (ll i = 0; i < n; i++) {
      auto [val, k, l, r] = v[i];
      if (val >= 0) {
        auto itr = st.lower_bound(k);
        if (itr != st.begin()) {
          ans += l;
          st.erase(--itr);
        } else {
          ans += r;
        }
      } else {
        break;
      }
    }
    set<ll> st2;
    for (ll i = 0; i < st.size(); i++) {
      st2.insert(n - 1 - i);
    }
    for (ll i = 0; i < n; i++) {
      auto [val, k, l, r] = v[i];
      if (val >= 0) {
        continue;
      } else {
        auto itr = st2.lower_bound(k);
        if (itr != st2.end()) {
          ans += r;
          st2.erase(itr);
        } else {
          ans += l;
        }
      }
    }
    cout << ans << endl;
  }
}
