#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll t;
  cin >> t;
  for (ll i = 0; i < (ll)(t); i++) {
    ll n;
    cin >> n;
    ll ans = 0;
    vector<vector<ll>> leftl(n);
    vector<vector<ll>> rightl(n);
    ll leftcnt = 0;
    ll rightcnt = 0;
    for (ll i = 0; i < (ll)(n); i++) {
      ll k, l, r;
      cin >> k >> l >> r;
      if (l == r)
        ans += l;
      else if (k == n)
        ans += l;
      else if (l > r) {
        ans += r;
        leftl[k - 1].push_back(l - r);
        leftcnt++;
      } else {
        ans += l;
        rightl[k - 1].push_back(r - l);
        rightcnt++;
      }
    }
    priority_queue<ll, vector<ll>, less<ll>> q1;
    priority_queue<ll, vector<ll>, less<ll>> q2;
    for (ll i = (n - 1); i >= (0); i--) {
      if (leftcnt == 0) break;
      for (ll j = 0; j < (ll)(leftl[i].size()); j++) q1.push(leftl[i][j]);
      if (q1.size()) {
        ans += q1.top();
        q1.pop();
        leftcnt--;
      }
    }
    for (ll i = (0); i <= (n - 1); i++) {
      if (rightcnt == 0) break;
      for (ll j = 0; j < (ll)(rightl[i].size()); j++) q2.push(rightl[i][j]);
      if (q2.size()) {
        ans += q2.top();
        q2.pop();
        rightcnt--;
      }
    }
    cout << ans << endl;
  }
}
