#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
void solve() {
  ll N;
  cin >> N;
  vector<P> num;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ll K, L, R;
    cin >> K >> L >> R;
    ans += L;
    num.push_back(P(L - R, K));
  }
  set<ll> s;
  for (int i = 1; i <= N; i++) {
    s.insert(-i);
  }
  ll canNotNum = 0;
  sort(num.rbegin(), num.rend());
  ll minusIndex = -1;
  for (int i = 0; i < N; i++) {
    if (num[i].first < 0) {
      minusIndex = i;
      break;
    }
    auto itr = s.lower_bound(-num[i].second);
    if (itr == s.end()) {
      canNotNum++;
      ans -= num[i].first;
    } else {
      s.erase(itr);
    }
  }
  if (minusIndex < 0) {
    cout << ans << "\n";
    return;
  }
  set<ll> minusPlace;
  for (int i = minusIndex; i < N; i++) {
    minusPlace.insert(minusIndex + 1);
  }
  for (int i = N - 1; i >= minusIndex; --i) {
    auto itr = minusPlace.upper_bound(num[i].second);
    if (itr != minusPlace.end()) {
      minusPlace.erase(itr);
      ans -= num[i].first;
    }
  }
  cout << ans << "\n";
  return;
}
int main() {
  ll T;
  cin >> T;
  for (int q = 0; q < T; q++) {
    solve();
  }
  return 0;
}
