#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  long long ans = 0;
  vector<long long> pref(n);
  vector<long long> prof(n);
  for (long long i = 0; i < n; i++) {
    long long k, l, r;
    cin >> k >> l >> r;
    k--;
    ans += r;
    l -= r;
    r = 0;
    pref[i] = k;
    prof[i] = l;
  }
  vector<vector<long long>> bpref(n);
  set<pair<long long, long long>> ispref;
  set<pair<long long, long long>> ntpref;
  for (long long i = 0; i < n; i++) {
    ntpref.insert({prof[i], i});
    bpref[pref[i]].push_back(i);
  }
  for (long long i = n - 1; i >= 0; i--) {
    for (auto j : bpref[i]) {
      if (ntpref.count({prof[j], j})) {
        ispref.insert({prof[j], j});
        ntpref.erase({prof[j], j});
      }
    }
    if (ispref.empty() || (!ntpref.empty() && ntpref.begin()->first < 0)) {
      ntpref.erase(ntpref.begin());
    } else {
      ans += ispref.begin()->first;
      ispref.erase(ispref.begin());
    }
  }
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long q;
  cin >> q;
  while (q--) {
    solve();
  }
}
