#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);
  long long cnt_pos = 0;
  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;
    cnt_pos += (prof[i] >= 0);
  }
  vector<vector<long long>> bpref(n);
  vector<vector<long long>> bsuff(n);
  for (long long i = 0; i < n; i++) {
    if (pref[i] == n - 1) {
      ans += prof[i];
      continue;
    }
    if (prof[i] >= 0) {
      bpref[pref[i]].push_back(i);
    } else {
      bsuff[pref[i] + 1].push_back(i);
    }
  }
  {
    set<pair<long long, long long>> s;
    for (long long i = n - 1; i >= 0; i--) {
      for (auto j : bpref[i]) {
        s.insert({prof[j], j});
      }
      if (i < cnt_pos && !s.empty()) {
        ans += s.begin()->first;
        s.erase(s.begin());
      }
    }
  }
  {
    set<pair<long long, long long>> s;
    for (long long i = 0; i < n; i++) {
      for (auto j : bsuff[i]) {
        s.insert({-prof[j], j});
      }
      if (i >= cnt_pos && !s.empty()) {
        s.erase(--s.end());
      }
    }
    for (auto t : s) {
      ans -= t.first;
    }
  }
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long q;
  cin >> q;
  while (q--) {
    solve();
  }
}
