#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
signed main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int T;
  cin >> T;
  for (int test = 0; test < (T); test++) {
    int n;
    cin >> n;
    vector<pair<int, pair<int, int>>> v;
    for (int i = 0; i < (n); i++) {
      int k, l, r;
      cin >> k >> l >> r;
      v.push_back({k, {l, r}});
    }
    sort((v).begin(), (v).end());
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        pq;
    vector<pair<int, pair<int, int>>> vec;
    long long ans = 0;
    for (int i = 0; i < (n); i++) {
      if (v[i].second.first > v[i].second.second) {
        if (v[i].first > pq.size()) {
          ans += v[i].second.first;
          pq.push({v[i].second.first - v[i].second.second, i});
        } else {
          if (pq.top().first < v[i].second.first - v[i].second.second) {
            vec.push_back(v[pq.top().second]);
            ans += v[i].second.first;
            ans -= v[pq.top().second].second.first;
            pq.pop();
            pq.push({v[i].second.first - v[i].second.second, i});
          } else {
            ans -= (v[i].second.first - v[i].second.second);
          }
        }
      } else
        vec.push_back(v[i]);
    }
    sort((vec).begin(), (vec).end(), greater<pair<int, pair<int, int>>>());
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        pqr;
    for (int i = 0; i < (vec.size()); i++) {
      if (n - vec[i].first > pqr.size()) {
        ans += vec[i].second.second;
        pqr.push({vec[i].second.second - vec[i].second.first, i});
      } else {
        if (pqr.size() != 0 &&
            pqr.top().first < vec[i].second.second - vec[i].second.first) {
          ans += vec[i].second.second;
          ans -= pqr.top().first;
          pqr.pop();
          pqr.push({vec[i].second.second - vec[i].second.first, i});
        } else {
          ans += vec[i].second.first;
        }
      }
    }
    cout << ans << endl;
  }
}
