#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7, N = 1e5 + 10;
template <typename T>
class SegTree {
 public:
  vector<T> t, lazy;
  vector<T> A;
  ll n;
  SegTree(ll n) {
    this->n = n;
    this->t.assign(4 * n + 10, 0);
    this->lazy.assign(4 * n + 10, 0);
  }
  SegTree(vector<T> a) {
    this->n = a.size();
    this->A = a;
    this->t.assign(4 * n, 0);
    this->lazy.assign(4 * n, 0);
    build(1, 0, n - 1);
  }
  T fun(T a, T b) { return min(a, b); }
  void build(ll v, ll tl, ll tr) {
    if (tl == tr) {
      t[v] = A[tl];
    } else {
      ll tm = (tl + tr) / 2;
      build(2 * v, tl, tm);
      build(2 * v + 1, tm + 1, tr);
      t[v] = fun(t[2 * v], t[2 * v + 1]);
    }
  }
  void update(ll v, ll tl, ll tr, ll pos, T new_val) {
    if (tl == tr) {
      t[v] = new_val;
    } else {
      ll tm = (tl + tr) / 2;
      if (pos <= tm)
        update(v * 2, tl, tm, pos, new_val);
      else
        update(v * 2 + 1, tm + 1, tr, pos, new_val);
      t[v] = fun(t[v * 2], t[v * 2 + 1]);
    }
  }
  T query(ll v, ll tl, ll tr, ll l, ll r) {
    if (l > r) return LLONG_MAX;
    if (l == tl && tr == r) return t[v];
    ll tm = (tl + tr) / 2;
    return fun(query(v * 2, tl, tm, l, min(r, tm)),
               query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
  }
  void update(ll i, T val) { update(1, 0, n - 1, i, val); }
  T query(ll l, ll r) { return query(1, 0, n - 1, l, r); }
  T query(ll i) { return query(i, i); }
};
void test() {
  ll n;
  cin >> n;
  vector<array<ll, 3>> A;
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ll k, l, r;
    cin >> k >> l >> r;
    if (k == n) {
      ans += l;
    } else {
      ll mn = min(l, r);
      l -= mn;
      r -= mn;
      ans += mn;
      if (l or r) A.push_back({k, l, r});
    }
  }
  if (A.empty()) {
    cout << ans << '\n';
    return;
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  ll en = n;
  n = A.size();
  set<pair<ll, ll>> done;
  set<ll> left;
  for (ll i = 1; i < en + 1; i++) left.insert(i);
  SegTree<ll> st(en + 5);
  for (ll i = 0; i < n; i++) {
    ll j = i;
    vector<ll> ff, ss;
    while (j < n and A[i][0] == A[j][0]) {
      if (A[j][1]) {
        ff.push_back(A[j][1]);
      }
      if (A[j][2]) {
        ss.push_back(A[j][2]);
      }
      j++;
    }
    sort(ff.begin(), ff.end());
    sort(ss.begin(), ss.end());
    while (!ss.empty()) {
      ll v = st.query(A[i][0] + 1, en);
      if (v >= ss.back()) break;
      if (v == 0) {
        auto k = *left.rbegin();
        st.update(k, ss.back());
        ans += ss.back();
        done.insert({ss.back(), k});
        left.erase(k);
      } else {
        auto k = done.upper_bound({v, -1});
        k--;
        st.update((*k).second, ss.back());
        auto R = *k;
        ans -= R.first;
        done.erase(k);
        R.first = ss.back();
        ans += R.first;
        done.insert(R);
      }
      ss.pop_back();
    }
    while (!ff.empty()) {
      ll v = st.query(1, A[i][0]);
      if (v >= ff.back()) break;
      if (v == 0) {
        auto k = left.upper_bound(A[i][0]);
        k--;
        st.update((*k), ff.back());
        ans += ff.back();
        done.insert({ff.back(), *k});
        left.erase(k);
      } else {
        auto k = done.lower_bound({v, -1});
        st.update((*k).second, ff.back());
        auto R = *k;
        ans -= R.first;
        done.erase(k);
        R.first = ff.back();
        ans += R.first;
        done.insert(R);
      }
      ff.pop_back();
    }
    i = j - 1;
  }
  cout << ans << '\n';
  return;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  ll tt = 1;
  cin >> tt;
  for (ll i = 0; i < tt; i++) test();
  return 0;
}
