#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll T;
  cin >> T;
  for (ll t = 0; t < T; t++) {
    ll N;
    cin >> N;
    vector<pair<ll, ll>> P(N);
    ll ans = 0;
    for (ll i = 0; i < N; i++) {
      ll k, l, r;
      cin >> k >> l >> r;
      ans += r;
      P[i] = make_pair(l - r, -k);
    }
    sort(P.begin(), P.end());
    ll c = 0;
    for (ll i = 0; i < N; i++) {
      while (true) {
        if (-P[c].second <= i + 1) {
          ans += P[c].first;
          c++;
          break;
        } else {
          break;
        }
        if (c == N - 1) break;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
