#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll T;
  cin >> T;
  for (ll j = 0; j < T; ++j) {
    ll N, ans = 0;
    cin >> N;
    vector<ll> BD(N + 10, 0);
    priority_queue<ll, vector<ll>, greater<ll>> LQ, RQ;
    for (ll i = 0; i < N; ++i) {
      ll K, L, R;
      cin >> K >> L >> R;
      if (L > R)
        LQ.push(L - R);
      else if (R > L && K < N)
        RQ.push(R - L);
      BD[K - 1]++;
      ans += min(R, L);
    }
    ll stock = LQ.size();
    for (ll i = 0; i < N; ++i) {
      stock -= BD[N - 1 - i];
      while (stock-- >= N - i && !LQ.empty()) LQ.pop();
      if (LQ.empty()) break;
    }
    stock = RQ.size();
    for (ll i = 1; i < N; i++) {
      stock -= BD[i - 1];
      while (stock-- >= N - i && !RQ.empty()) RQ.pop();
      if (RQ.empty()) break;
    }
    while (!LQ.empty()) {
      ans += LQ.top();
      LQ.pop();
    }
    while (!RQ.empty()) {
      ans += RQ.top();
      RQ.pop();
    }
    cout << ans << endl;
  }
}
