#include <bits/stdc++.h>
#define INF 5000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll T;
  cin >> T;
  for (ll testcase = 0; testcase < T; ++testcase) {
    ll N;
    cin >> N;
    vector<vector<ll>> left(N);
    vector<vector<ll>> right(N);
    ll ans = 0;
    for (ll i = 0; i < N; ++i) {
      ll K, L, R;
      cin >> K >> L >> R;
      K -= 1;
      ans += min(L, R);
      if (L > R) {
        left.at(K).push_back(L - R);
      }
      else {
        if (K != N - 1) {
          right.at(K + 1).push_back(R - L);
        }
      }
    }
    multiset<ll> ls;
    for (ll i = 0; i < N; ++i) {
      for (ll j = 0; j < left.at(i).size(); ++j) {
        ls.insert(left.at(i).at(j));
      }
      while (ls.size() > (i + 1)) {
        ls.erase(ls.begin());
      }
    }
    for (auto itr = ls.begin(); itr != ls.end(); ++itr) {
      ans += *(itr);
    }
    multiset<ll> rs;
    for (ll i = N - 1; i >= 0; --i) {
      for (ll j = 0; j < right.at(i).size(); ++j) {
        rs.insert(right.at(i).at(j));
      }
      while (rs.size() > (N - 1 - i + 1)) {
        rs.erase(rs.begin());
      }
    }
    for (auto itr = rs.begin(); itr != rs.end(); ++itr) {
      ans += *(itr);
    }
    cout << ans << "\n";
  }
}
