#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int i = 0; i < T; i++) {
    long long N;
    cin >> N;
    long long ans = 0;
    vector<pair<long long, pair<long long, long long> > > l, r;
    for (int j = 0; j < N; j++) {
      long long K, L, R;
      cin >> K >> L >> R;
      K--;
      if (L >= R) {
        l.push_back(make_pair(K, pair<long long, long long>(L, R)));
        ans += R;
      } else {
        if (K != N - 1)
          r.push_back(make_pair(K, pair<long long, long long>(L, R)));
        ans += L;
      }
    }
    priority_queue<long long> Q1, Q2;
    sort(l.begin(), l.end());
    sort(r.begin(), r.end());
    reverse(r.begin(), r.end());
    int idx = 0;
    for (int j = 0; j < N; j++) {
      while (idx < l.size() && l[idx].first == j) {
        Q1.push(l[idx].second.first - l[idx].second.second);
        idx++;
      }
      while (Q1.size() > j + 1) {
        Q1.pop();
      }
    }
    while (!Q1.empty()) {
      ans += Q1.top();
      Q1.pop();
    }
    idx = 0;
    for (int j = 0; j < N; j++) {
      while (idx < r.size() && r[idx].first == N - 2 - j) {
        Q2.push(r[idx].second.second - r[idx].second.first);
        idx++;
      }
      while (Q2.size() > j + 1) {
        Q2.pop();
      }
    }
    while (!Q2.empty()) {
      ans += Q2.top();
      Q2.pop();
    }
    cout << ans << endl;
  }
  return 0;
}
