#include <bits/stdc++.h>
using namespace std;
int T, N;
int main() {
  cin >> T;
  for (; T--;) {
    cin >> N;
    vector<pair<int, pair<int, int> > > L[2];
    long ans = 0;
    for (int i = 0; i < N; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l > r) {
        L[0].push_back(make_pair(k, make_pair(l, r)));
      } else if (l < r) {
        L[1].push_back(make_pair(N - k, make_pair(r, l)));
      } else
        ans += l;
    }
    for (int id = 0; id < 2; id++) {
      priority_queue<pair<int, int> > P;
      vector<vector<pair<int, int> > > tmp(N);
      for (pair<int, pair<int, int> > p : L[id]) {
        if (p.first == 0)
          ans += p.second.second;
        else
          tmp[p.first - 1].push_back(p.second);
      }
      for (int i = N; i--;) {
        for (pair<int, int> p : tmp[i]) P.push(p);
        if (i < L[id].size() && !P.empty()) {
          ans += P.top().first;
          P.pop();
        }
      }
      while (!P.empty()) {
        ans += P.top().second;
        P.pop();
      }
    }
    cout << ans << endl;
  }
}
