#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto&(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  vector<ll> k(n), l(n), r(n);
  vector<vector<ll>> L(n + 1), R(n + 1);
  REP(i, n) {
    cin >> k[i] >> l[i] >> r[i];
    ll mini = min(l[i], r[i]);
    ans += mini;
    l[i] -= mini;
    r[i] -= mini;
    if (l[i] > r[i]) {
      L[k[i]].push_back(i);
    } else if (l[i] < r[i]) {
      R[k[i]].push_back(i);
    }
  }

  priority_queue<P> pq;

  ll now = 1;
  FOR(i, 1, n + 1) {
    for (ll x : L[i]) {
      pq.push({m[x], x});
    }

    while (!pq.empty()) {
      P p = pq.front();
      pq.pop();
      if (p.second >= now) {
        ans += m[p.second];
        now++;
      }
    }
  }

  now = n;
  IFOR(i, 1, n + 1) {
    for (ll x : R[i]) {
      pq.push({m[x], x});
    }

    while (!pq.empty()) {
      P p = pq.front();
      pq.pop();
      if (p.second < now) {
        ans += m[p.second];
        break;
      }
    }
  }

  cout << ans << endl;
}

int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
}