#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll t1, t2, t3;
const ll mod = 1000000007;
const int INF = 1e9;
const ll INFLONG = 1e18;
const bool operator<(pair<ll, ll> pa1, pair<ll, ll> pa2) {
  return pa1.second > pa2.second;
}
int main() {
  ll t;
  cin >> t;
  ll k, l, r;
  for (int _ = 0; _ < t; _++) {
    ll n;
    cin >> n;
    ll sum = 0;
    vector<pair<ll, ll>> vec1;
    vector<pair<ll, ll>> vec2;
    for (int i = 0; i < n; i++) {
      cin >> k >> l >> r;
      k--;
      if (l >= r) {
        vec1.emplace_back(make_pair(k, l - r));
        sum += r;
      } else {
        if (k + 1 != n) {
          vec2.emplace_back(make_pair(k, r - l));
        }
        sum += l;
      }
    }
    ll len1 = vec1.size();
    ll len2 = vec2.size();
    sort((vec1).begin(), (vec1).end());
    sort((vec2).begin(), (vec2).end());
    priority_queue<ll> pq;
    ll piv = 0;
    ll num = 0;
    for (int i = 0; i < n; i++) {
      while (piv != len1 && vec1[piv].first == i) {
        pq.push(-vec1[piv].second);
        piv++;
        num++;
      }
      while (num > i + 1) {
        pq.pop();
        num--;
      }
    }
    while (!pq.empty()) {
      sum -= pq.top();
      pq.pop();
    }
    piv = len2 - 1;
    num = 0;
    for (int(i) = (int)(n)-1; (i) >= 0; --(i)) {
      while (piv != -1 && vec2[piv].first == i) {
        pq.push(-vec2[piv].second);
        piv--;
        num++;
      }
      while (num > n - i) {
        pq.pop();
        num--;
      }
    }
    while (!pq.empty()) {
      sum -= pq.top();
      pq.pop();
    }
    cout << sum << endl;
  }
}
