#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(51);
bool cmp(const pair<long long, pair<long long, long long>> &a,
         const pair<long long, pair<long long, long long>> &b) {
  return a.second.first - a.second.second > b.second.first - b.second.second;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<pair<long long, pair<long long, long long>>> v;
    set<pair<long long, long long>> s, s1;
    vector<long long> take(n), rr(n), kk(n), ll(n);
    for (long long i = 0; i < n; i++) {
      long long k, l, r;
      cin >> k >> l >> r;
      if (l >= r) {
        v.push_back({k, {l, r}});
      } else {
        s.insert({-(l - r), i});
        s1.insert({k, i});
        rr[i] = r;
        kk[i] = k;
        ll[i] = l;
        take[i] = l - r;
      }
    }
    sort(v.begin(), v.end(), cmp);
    set<long long> st;
    long long ans = 0;
    for (long long i = 1; i <= n; i++) {
      st.insert(i);
    }
    for (long long i = 0; i < v.size(); i++) {
      if (*st.begin() <= v[i].first) {
        auto it = st.upper_bound(v[i].first);
        it--;
        st.erase(it);
        ans += v[i].second.first;
      } else {
        ans += v[i].second.second;
      }
    }
    long long now = v.size();
    while (s.size() > 0) {
      while (s1.size() > 0 && s1.begin()->first <= now) {
        long long i = s1.begin()->second;
        s1.erase(*s1.begin());
        s.erase({-take[i], i});
        ans += rr[i];
        now++;
      }
      if (s.size() == 0) {
        break;
      }
      auto it = s.begin();
      long long i = it->second;
      ans += ll[i];
      now++;
      s.erase(s.begin());
      s1.erase({kk[i], i});
    }
    cout << ans << endl;
  }
  return 0;
}
