#include <bits/stdc++.h>
using namespace std;
int N_MAX = 200000;
int main() {
  int t, n, k, l, r, tt, i, j;
  long long res;
  int c[N_MAX];
  list<int> e;
  vector<pair<long long, int> > p;
  decltype(e)::iterator it;
  cin >> t;
  for (tt = 0; tt < t; tt++) {
    cin >> n;
    res = 0;
    fill(c, c + n, -1);
    e.clear();
    p.clear();
    for (i = 0; i < n; i++) {
      cin >> k >> l >> r;
      res += r;
      p.emplace_back(l - r, k - 1);
      e.push_back(i);
    }
    sort(p.begin(), p.end(), greater<pair<long long, int> >());
    for (i = 0; i < n; i++) {
      if (p[i].first <= 0) break;
      it = upper_bound(e.begin(), e.end(), p[i].second);
      if (it == e.begin()) {
        continue;
      }
      e.erase(prev(it));
      res += p[i].first;
    }
    j = e.size();
    e.clear();
    for (i = n - 1; j > 0; i--, j--) e.push_back(i);
    for (i = n - 1; i >= 0; i--) {
      if (p[i].first >= 0) break;
      it = upper_bound(e.begin(), e.end(), p[i].second + 1, greater<int>());
      if (it == e.begin()) {
        res += p[i].first;
        continue;
      }
      e.erase(prev(it));
    }
    cout << res << endl;
  }
  return 0;
}
