#include <bits/stdc++.h>
using namespace std;
template <typename... Args>
void db(Args... args) {}
int32_t main() {
  long long T;
  cin >> T;
  while (T--) {
    long long N;
    cin >> N;
    vector<pair<long long, pair<long long, long long>>> v1;
    vector<pair<long long, pair<long long, long long>>> v2;
    for (long long i = 0; i < N; i++) {
      long long K, L, R;
      cin >> K >> L >> R;
      if (L > R)
        v1.push_back({K, {L - R, R}});
      else
        v2.push_back({K, {R - L, L}});
    }
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    reverse(v2.begin(), v2.end());
    long long res = 0;
    long long sz1 = v1.size();
    long long sz2 = v2.size();
    set<pair<long long, long long>> st1, st2;
    for (long long i = 1; i <= sz1; i++) {
      while (v1.size() && v1.back().first >= i) {
        st1.insert({v1.back().second.first, v1.back().second.second});
        v1.pop_back();
      }
      if (st1.size()) {
        pair<long long, long long> ppp = *(--st1.end());
        res += ppp.first + ppp.second;
        st1.erase(--st1.end());
      }
    }
    for (auto x : v1) {
      res += x.second.second;
    }
    for (auto x : st1) {
      res += x.second;
    }
    for (long long i = sz1 + 1; i <= N; i++) {
      while (v2.size() && v2.back().first < i) {
        st2.insert({v2.back().second.first, v2.back().second.second});
        v2.pop_back();
      }
      if (st2.size()) {
        pair<long long, long long> ppp = *(--st2.end());
        res += ppp.first + ppp.second;
        st2.erase(--st2.end());
      }
    }
    for (auto x : v2) {
      res += x.second.second;
    }
    for (auto x : st2) {
      res += x.second;
    }
    cout << res << '\n';
  }
  return 0;
}
