#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15;
const long long MOD = 1e9 + 7;
int main() {
  long long i, j, k;
  long long T;
  cin >> T;
  long long t;
  for (t = 0; t < T; t++) {
    long long N;
    cin >> N;
    vector<long long> K, L, R;
    map<long long, set<long long>> lmap, rmap;
    for (i = 0; i < N; i++) {
      long long k, l, r;
      cin >> k >> l >> r;
      k--;
      K.push_back(k);
      L.push_back(l);
      R.push_back(r);
    }
    long long maxlk = 0, minrk = N;
    for (i = 0; i < N; i++) {
      if (L[i] >= R[i] || K[i] == N - 1) {
        maxlk = max(maxlk, K[i]);
        if (lmap.count(K[i]) == 0) {
          lmap.insert(make_pair(K[i], set<long long>()));
        }
        if (K[i] == N - 1) {
          lmap[K[i]].insert(0);
        } else {
          lmap[K[i]].insert(L[i] - R[i]);
        }
      } else {
        minrk = min(minrk, K[i] + 1);
        if (rmap.count(K[i] + 1) == 0) {
          rmap.insert(make_pair(K[i] + 1, set<long long>()));
        }
        rmap[K[i] + 1].insert(R[i] - L[i]);
      }
    }
    long long ans = 0;
    set<long long> now;
    for (j = maxlk; j >= 0; j--) {
      if (lmap.count(j)) {
        set<long long> uni;
        set_union(now.begin(), now.end(), lmap[j].begin(), lmap[j].end(),
                  inserter(uni, uni.end()));
        now = uni;
      }
      if (now.size() > 0) {
        auto itr = now.end();
        itr--;
        ans += *itr;
        now.erase(itr);
      }
    }
    now = set<long long>();
    for (j = minrk; j < N; j++) {
      if (rmap.count(j)) {
        set<long long> uni;
        set_union(now.begin(), now.end(), rmap[j].begin(), rmap[j].end(),
                  inserter(uni, uni.end()));
        now = uni;
      }
      if (now.size() > 0) {
        auto itr = now.end();
        itr--;
        ans += *itr;
        now.erase(itr);
      }
    }
    for (i = 0; i < N; i++) {
      if (K[i] == N - 1) {
        ans += L[i];
      } else {
        ans += min(L[i], R[i]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
