#include <bits/stdc++.h>
#pragma GCC optimize("O3,Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, tot = 0;
    cin >> n;
    vector<pair<long long, pair<long long, long long>>> a;
    vector<pair<long long, pair<long long, long long>>> b;
    for (long long i = 0; i < n; i++) {
      long long k, l, r;
      cin >> k >> l >> r;
      tot += max(l, r);
      if (l > r) {
        a.push_back({k, {l, r}});
      } else {
        b.push_back({n - k, {l, r}});
      }
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    set<long long> fuck;
    long long curr = 0;
    for (auto pr : a) {
      fuck.insert(pr.second.first - pr.second.second);
      long long k = pr.first;
      curr++;
      while (curr > k) {
        long long x = *(fuck.begin());
        fuck.erase(x);
        tot -= x;
        curr--;
      }
    }
    fuck.clear();
    curr = 0;
    for (auto pr : b) {
      fuck.insert(pr.second.second - pr.second.first);
      long long k = pr.first;
      curr++;
      while (curr > k) {
        long long x = *(fuck.begin());
        fuck.erase(x);
        tot -= x;
        curr--;
      }
    }
    cout << tot << '\n';
  }
  fflush(stdout);
  return 0;
}
