#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  long long k[n], l[n], r[n];
  for (long long i = 0; i <= n - 1; i++) cin >> k[i] >> l[i] >> r[i];
  long long ans = 0;
  for (long long i = 0; i <= n - 1; i++) ans += min(r[i], l[i]);
  multiset<long long> q;
  vector<long long> q1[n];
  for (long long i = 0; i < n; i++) {
    q1[k[i] - 1].push_back(i);
  }
  for (long long i = 0; i <= n - 1; i++) {
    if ((l[i] < r[i]) and (k[i] != n)) q.insert(r[i] - l[i]);
  }
  map<long long, long long> q2;
  for (long long i = n - 1; i >= 0; i--) {
    for (auto it : q1[i]) {
      if (l[it] > r[it])
        q.insert(l[it] - r[it]);
      else {
        if (q2[r[it] - l[it]] == 0)
          q.erase(q.find(r[it] - l[it]));
        else
          q2[r[it] - l[it]]--;
      }
    }
    ans += *q.rbegin();
    q2[*q.rbegin()]++;
    if (q.size()) q.erase(q.find(*q.rbegin()));
  }
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long t = 1;
  cin >> t;
  while (t--) solve();
}
