#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
void solve() {
  int n;
  cin >> n;
  priority_queue<ll, vector<ll>, function<bool(ll, ll)>> pq(
      [](ll a, ll b) { return a > b; });
  priority_queue<ll> pq2;
  vector<pll> vec;
  ll k, l, r, tmp;
  ll ans = 0;
  for (int(i) = (0); (i) < (n); (i)++) {
    cin >> k >> l >> r;
    ans += min(l, r);
    tmp = l - r;
    vec.push_back(make_pair(k, tmp));
  }
  sort(vec.begin(), vec.end(),
       [](const pll &a, const pll &b) { return a.first < b.first; });
  int pos = 0;
  for (int(j) = (1); (j) < (n + 1); (j)++) {
    while (vec[pos].first == j) {
      if (vec[pos].second >= 0) {
        pq.push(vec[pos].second);
      }
      pos++;
    }
    while (pq.size() > j) {
      pq.pop();
    }
  }
  while (!pq.empty()) {
    ans += pq.top();
    pq.pop();
  }
  pos = n - 1;
  for (int j = n; j > 0; j--) {
    while (vec[pos].first == j) {
      if (vec[pos].second < 0) {
        pq2.push(vec[pos].second);
      }
      pos--;
    }
    while (pq2.size() > n - j) {
      pq2.pop();
    }
  }
  while (!pq2.empty()) {
    ans -= pq2.top();
    pq2.pop();
  }
  cout << ans << endl;
}
int main() {
  int t;
  cin >> t;
  for (int(i) = (0); (i) < (t); (i)++) {
    solve();
  }
  return 0;
}
