#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
  int n;
  cin >> n;
  ll sum = 0;
  vector<vector<ll>> ev(n);
  for (int i = 0; i < n; ++i) {
    int place;
    cin >> place;
    ll inPref, outPref;
    cin >> inPref >> outPref;
    sum += outPref;
    ev[place - 1].push_back(inPref - outPref);
  }
  priority_queue<ll> disp;
  priority_queue<ll, vector<ll>, greater<ll>> cur;
  for (int iPlace = 0; iPlace < n; ++iPlace) {
    for (int x : ev[iPlace]) {
      disp.push(x);
    }
    while (!disp.empty() && !cur.empty() && disp.top() > 0 && cur.top() < 0) {
      cur.pop();
      cur.push(disp.top());
      disp.pop();
    }
    while (!disp.empty() && ((int)((cur).size())) <= iPlace) {
      cur.push(disp.top());
      disp.pop();
    }
    while (!disp.empty() && !cur.empty() && disp.top() > cur.top()) {
      cur.pop();
      cur.push(disp.top());
      disp.pop();
    }
  }
  while (!cur.empty()) {
    sum += cur.top();
    cur.pop();
  }
  cout << sum << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int nbTests;
  cin >> nbTests;
  for (int iTest = 0; iTest < nbTests; ++iTest) {
    solve();
  }
}
