#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;
};
bool intersects(ll a, ll b, ll c, ll d) {
  assert(a <= b && c <= d);
  return !(b <= c || a >= d);
}
struct segtree {
  segtree(ll l_, ll r_, ll v, segtree* L_, segtree* R_)
      : l(l_), r(r_), val(v), L(L_), R(R_) {}
  void add(ll a, ll b, ll v) {
    assert(intersects(a, b, l, r));
    if (a <= l && r <= b) {
      lazy += v;
      val += v;
      return;
    }
    L->lazy += lazy;
    L->val += lazy;
    R->lazy += lazy;
    R->val += lazy;
    lazy = 0;
    if (intersects(a, b, L->l, L->r)) {
      L->add(a, b, v);
    }
    if (intersects(a, b, R->l, R->r)) {
      R->add(a, b, v);
    }
    val = std::min(L->val, R->val);
  }
  ll min() { return val; }
  ll l;
  ll r;
  ll val;
  ll lazy;
  segtree* L;
  segtree* R;
};
segtree* build(ll l, ll r) {
  if (l + 1 == r) {
    return new segtree{l, r, l + 1, nullptr, nullptr};
  } else {
    ll m = (l + r) / 2;
    segtree* L = build(l, m);
    segtree* R = build(m, r);
    return new segtree{l, r, min(L->val, R->val), L, R};
  }
}
int main() {
  ll T;
  cin >> T;
  for (ll cas = 1; cas <= T; cas++) {
    ll n;
    cin >> n;
    vector<llll> C;
    for (ll i = 0; i < n; i++) {
      ll k, l, r;
      cin >> k >> l >> r;
      C.push_back({abs(r - l), k, l, r});
    }
    sort(C.begin(), C.end());
    segtree* P = build(0, n);
    segtree* S = build(0, n);
    ll ans = 0;
    for (ll i = n - 1; i >= 0; i--) {
      auto [diff, k, L, R] = C[i];
      ll kr = n - k;
      (void)diff;
      if (kr == 0) {
        ans += L;
      } else if (L > R) {
        P->add(k - 1, n, -1);
        if (P->min() < 0) {
          ans += R;
          P->add(k - 1, n, 1);
        } else {
          ans += L;
        }
      } else {
        S->add(kr - 1, n, -1);
        if (S->min() < 0) {
          ans += L;
          S->add(kr - 1, n, 1);
        } else {
          ans += R;
        }
      }
      assert(P->min() >= 0);
      assert(S->min() >= 0);
    }
    cout << ans << endl;
  }
}
