#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, t;
  cin >> t;
  set<int> sp, sm;
  set<pair<long long, long long> > p, m;
  while (t--) {
    cin >> n;
    long long ans = 0LL;
    sp.clear();
    sm.clear();
    p.clear();
    m.clear();
    vector<long long> k(n), l(n), r(n), a(n), f(n);
    for (int i = 0; i < n; i++) {
      a[i] = 0;
      f[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      cin >> k[i] >> l[i] >> r[i];
      ans += r[i];
      k[i]--;
      if (-(r[i] - l[i]) < 0LL) {
        m.insert({-(r[i] - l[i]), k[i]});
      } else {
        p.insert({-(r[i] - l[i]), k[i]});
      }
    }
    for (int i = 0; i < n; i++) {
      if (i < p.size()) {
        sp.insert(-i);
      } else {
        sm.insert(i);
      }
    }
    set<pair<long long, long long> >::reverse_iterator it = p.rbegin();
    for (; it != p.rend(); it++) {
      if (sp.lower_bound(-it->second) != sp.end()) {
        ans += it->first;
        sp.erase(sp.lower_bound(-it->second));
      } else {
        sp.erase(sp.begin());
      }
    }
    set<pair<long long, long long> >::iterator it2 = m.begin();
    for (; it2 != m.end(); it2++) {
      if (sm.upper_bound(it2->second) != sm.end()) {
        sm.erase(sm.upper_bound(it2->second));
      } else {
        sm.erase(sm.begin());
        ans += it2->first;
      }
    }
    cout << ans << endl;
  }
}
