#include <bits/stdc++.h>
using namespace std;
 
using ll = int64_t;
constexpr ll mod = ll(1e9 + 7);
 

using tup = tuple<int,int>;
void AddToLo(int i, priority_queue<tup, vector<tup>, greater<tup>>& lo, vector<bool>& add_lo_ans) {
  while (i >= 0 && !add_lo_ans[i]) {
    add_lo_ans[i] = true;
    lo.emplace(INT_MIN, i);
    --i;
  }
}

void AddToLoFromTop(int i, priority_queue<tup, vector<tup>, greater<tup>>& lo, vector<tuple<ll,ll,ll,ll>>& ans) {
  if (get<2>(ans[i]) == -1) {
    lo.emplace(INT_MIN, i);
  } else {
    lo.emplace(get<3>(ans[i]), i);
  }
}

int main () {
  ios::sync_with_stdio(0); cin.tie(0);
 
  ll TT = 1;
  cin >> TT;
  vector<tuple<ll,ll,ll>> A;
  vector<vector<tuple<ll,ll,ll>>> before, after;
  vector<int> extra;
  vector<tuple<ll,ll,ll,ll>> ans;

  while (TT--) {
    ll n;
    cin >> n;
    A.resize(n);
    for (auto& [k,l,r] : A) cin >> k >> l >> r;
    extra.clear();
    before.clear();
    after.clear();
    before.resize(n+1); // need to put value [0,i] inclusive
    after.resize(n+1); // need to put value [i,n-1] inclusive
    for (int i = 0; i < n; ++i) {
      auto [k,l,r] = A[i];
      if (l > r) {
        before[k-1].emplace_back(l,r,i);
      } else if (l < r) {
        after[k].emplace_back(l,r,i);
      } else {
        extra.emplace_back(i);
      }
    }

    for (int i = 0; i <= n; ++i) {
      sort(before[i].begin(), before[i].end(), [](const auto& a, const auto& b) {
                                                 int da = get<0>(a) - get<1>(a);
                                                 int db = get<0>(b) - get<1>(b);
                                                 return da > db;
                                               });
      sort(after[i].begin(), after[i].end(), [](const auto& a, const auto& b) {
                                                 int da = get<1>(a) - get<0>(a);
                                                 int db = get<1>(b) - get<0>(b);
                                                 return da > db;
                                               });
    }
    ans.clear();
    ans.resize(n, {-1,-1,-1,-1});

    priority_queue<tup, vector<tup>, greater<tup>> lo;
    vector<bool> add_lo_ans(n);
    for (int k = 0; k <= n; ++k) {
      AddToLo(k, lo, add_lo_ans);
      for (auto [l,r,ii] : before[k]) {
        if (lo.empty()) break;
        auto [dx,i_min] = lo.top();
        if (dx >= l-r) break;
        lo.pop();
        ans[i_min] = {l, r, ii, l-r};
        lo.emplace(l-r, i_min);
      }
    }
    // lo.clear();
    while (lo.size()) lo.pop();

    // move max unused before forward to lower places

    for (int k = n-1; k >= 0; --k) {
      AddToLoFromTop(k, lo, ans);
      for (auto [l,r,ii] : after[k]) {
        
        if (lo.empty()) break;
        auto [dx,i_min] = lo.top();
        if (dx >= l-r) break;
        lo.pop();
        ans[i_min] = {l, r, ii, l-r};
        lo.emplace(l-r, i_min);
      }
    }
    vector<bool> used(n);

    ll out = 0;
    for (int ii = 0; ii < n; ++ii) {
      if (get<2>(ans[ii]) == -1) continue;
      int i = get<2>(ans[ii]);
      out += max(get<1>(A[i]), get<2>(A[i]));
      used[i] = true;
    }


    for (int i = 0; i < n; ++i) {
      if (!used[i]) {
        out += min(get<1>(A[i]), get<2>(A[i]));
      }
    }

    cout << out << endl;
  }
}
