#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vector<T>>;
template <class T>
inline bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
using arr = array<ll, 3>;
void solve() {
  ll N;
  cin >> N;
  vc<arr> A(N);
  vvc<arr> S1(N), S2(N);
  for (ll i = 0; i < (N); i++) {
    cin >> A[i][0] >> A[i][1] >> A[i][2], A[i][0]--;
    if (A[i][1] >= A[i][2] or A[i][0] == N - 1)
      S1[A[i][0]].emplace_back(A[i]);
    else
      S2[N - A[i][0] - 1].emplace_back(A[i]);
  }
  auto comp1 = [&](arr a, arr b) { return (a[1] - a[2]) > (b[1] - b[2]); };
  auto comp2 = [&](arr a, arr b) { return (a[1] - a[2]) < (b[1] - b[2]); };
  priority_queue<arr, vc<arr>, decltype(comp1)> pq1{comp1};
  priority_queue<arr, vc<arr>, decltype(comp2)> pq2{comp2};
  ll ans = 0, cnt = 0;
  for (ll i = 0; i < (N); i++) {
    for (auto& a : S1[i]) pq1.emplace(a), cnt++;
    while (i < cnt - 1) {
      auto [k, l, r] = pq1.top();
      pq1.pop();
      cnt--;
      ans += r;
    }
  }
  while (!pq1.empty()) {
    auto [k, l, r] = pq1.top();
    pq1.pop();
    ans += l;
  }
  cnt = 0;
  for (ll i = 0; i < (N); i++) {
    for (auto& a : S2[i]) pq2.emplace(a), cnt++;
    while (i < cnt - 1) {
      auto [k, l, r] = pq2.top();
      pq2.pop();
      cnt--;
      ans += l;
    }
  }
  while (!pq2.empty()) {
    auto [k, l, r] = pq2.top();
    pq2.pop();
    ans += r;
  }
  cout << ans << endl;
}
int main() {
  init();
  ll T;
  cin >> T;
  for (ll _ = 0; _ < (T); _++) solve();
}
