#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  for (; t; t--) {
    long long n;
    cin >> n;
    vector<long long> k(n), l(n), r(n);
    vector<pair<long long, long long>> vl(n);
    for (long long i = 0; i < n; i++) {
      cin >> k[i] >> l[i] >> r[i];
      k[i]--;
      vl[i] = {(l[i] - r[i]), i};
    }
    sort(vl.rbegin(), vl.rend());
    long long ans = 0;
    set<long long> q;
    for (long long i = 0; i < n; i++) q.insert(i);
    for (long long i = 0; i < n; i++) {
      if (vl[i].first <= 0) break;
      long long id = vl[i].second;
      auto t = upper_bound(q.begin(), q.end(), k[id]);
      if (t == q.begin()) {
        ans += r[id];
      } else {
        ans += l[id];
        t--;
        q.erase(t);
      }
    }
    for (long long i = 0; i < n; i++) q.insert(i);
    for (long long i = n - 1; i >= 0; i--) {
      if (vl[i].first > 0) break;
      long long id = vl[i].second;
      auto t = upper_bound(q.begin(), q.end(), k[id]);
      if (t == q.end())
        ans += l[id];
      else {
        ans += r[id];
        q.erase(t);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
