#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > l1;
priority_queue<long long, vector<long long>, greater<long long> > q1;
vector<pair<long long, long long> > l2;
priority_queue<long long, vector<long long>, greater<long long> > q2;
signed main() {
  long long t, n, k, l, r, s1, s2;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    for (long long i = 0; i < n; i++) {
      cin >> k >> l >> r;
      if (l < r) {
        l2.push_back({n - k + 1, r - l});
      } else
        l1.push_back({k, l - r});
      s1 += min(l, r);
    }
    sort(l1.begin(), l1.end());
    sort(l2.begin(), l2.end());
    for (long long i = 0; i < l1.size(); i++) {
      if (q1.size() < l1[i].first)
        q1.push(l1[i].second);
      else if (q1.top() < l1[i].second) {
        q1.pop();
        q1.push(l1[i].second);
      }
    }
    while (q1.size()) {
      s1 += q1.top();
      q1.pop();
    }
    for (long long i = 0; i < l2.size(); i++) {
      if (q2.size() < l2[i].first)
        q2.push(l2[i].second);
      else if (q2.top() < l2[i].second) {
        q2.pop();
        q2.push(l2[i].second);
      }
    }
    while (q2.size()) {
      s1 += q2.top();
      q2.pop();
    }
    vector<pair<long long, long long> >().swap(l1);
    vector<pair<long long, long long> >().swap(l2);
  }
}
