#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using llll = tuple<ll, ll, ll, ll>;
ll LSB(ll n) { return n & (-n); }
struct fenwick {
  fenwick(ll n) : F(n + 1, 0) {}
  void add(ll i, ll x) {
    while (i < F.size()) {
      F[i] += x;
      i += LSB(i);
    }
  }
  ll sum(ll i) {
    ll ans = 0;
    while (i > 0) {
      ans += F[i];
      i -= LSB(i);
    }
    return ans;
  }
  vector<ll> F;
};
int main() {
  ll T;
  cin >> T;
  for (ll cas = 1; cas <= T; cas++) {
    ll n;
    cin >> n;
    set<ll> AVAIL;
    vector<llll> C;
    for (ll i = 0; i < n; i++) {
      AVAIL.insert(i);
      ll k, l, r;
      cin >> k >> l >> r;
      C.push_back({abs(r - l), k, l, r});
    }
    sort(C.begin(), C.end());
    fenwick P(n);
    fenwick S(n);
    ll ans = 0;
    for (ll i = n - 1; i >= 0; i--) {
      auto [diff, k, L, R] = C[i];
      (void)diff;
      if (L > R) {
        if (P.sum(k) < k) {
          ans += L;
          P.add(k, 1);
        } else {
          ans += R;
        }
      } else {
        ll kr = n - k;
        if (S.sum(kr) < kr) {
          ans += R;
          S.add(kr, 1);
        } else {
          ans += L;
        }
      }
    }
    cout << ans << endl;
  }
}
