#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> k(n), l(n), r(n);
    for (int i = 0; i < n; i++) cin >> k[i] >> l[i] >> r[i];
    long long s = 0;
    for (int i = 0; i < n; i++) s = s + l[i];
    vector<pair<int, int>> v(n);
    for (int i = 0; i < n; i++) v[i] = make_pair(k[i], l[i] - r[i]);
    sort(v.begin(), v.end());
    int tt = 0;
    priority_queue<pair<long long, long long>,
                   vector<pair<long long, long long>>,
                   greater<pair<long long, long long>>>
        pq;
    vector<pair<long long, long long>> v1;
    for (int i = 0; i < n; i++) {
      if (tt < v[i].first) {
        if (v[i].second < 0) {
          s = s - v[i].second;
          v1.push_back(v[i]);
        } else {
          tt += 1;
          pq.push(make_pair(v[i].second, v[i].first));
        }
      } else if (tt == v[i].first) {
        if (v[i].second < 0) {
          s = s - v[i].second;
          v1.push_back(v[i]);
          continue;
        }
        pair<long long, long long> y = pq.top();
        if (y.first < v[i].second) {
          s = s - y.first;
          pq.pop();
          pq.push(make_pair(v[i].second, v[i].first));
          v1.push_back({y.second, y.first});
        } else {
          s = s - v[i].second;
          v1.push_back(v[i]);
        }
      }
    }
    sort(v1.begin(), v1.end());
    int i = 0;
    for (; i < v1.size(); i++) {
      if (tt >= v1[i].first)
        tt += 1;
      else
        break;
    }
    priority_queue<pair<long long, long long>,
                   vector<pair<long long, long long>>,
                   greater<pair<long long, long long>>>
        pq1;
    for (; i < v1.size(); i++) {
      if (tt < v1[i].first) {
        tt += 1;
        s = s + v1[i].second;
        pq1.push({v1[i].second, v1[i].first});
      } else {
        pair<long long, long long> p = pq1.top();
        pq1.pop();
        if (p.first < v1[i].second) {
          s = s - p.first;
          s = s + v1[i].second;
          pq1.push({v1[i].second, v1[i].first});
        }
        tt += 1;
      }
    }
    cout << s << "\n";
  }
}
