#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    int N;
    cin >> N;
    vector<long long> KL, KR;
    vector<long long> scL, scR;
    long long ans = 0;
    for (int i = 0; i < N; i++) {
      long long Ktmp, Ltmp, Rtmp;
      cin >> Ktmp >> Ltmp >> Rtmp;
      if (Ltmp >= Rtmp) {
        ans += Rtmp;
        KL.push_back(Ktmp);
        scL.push_back(Ltmp - Rtmp);
      } else {
        ans += Ltmp;
        KR.push_back(Ktmp);
        scR.push_back(Rtmp - Ltmp);
      }
    }
    priority_queue<long long, vector<long long>, greater<long long>> scL_que,
        scR_que;
    int NL = KL.size();
    int NR = KR.size();
    vector<vector<long long>> K_to_scL(N + 1), K_to_scR(N + 1);
    for (int i = 0; i < NL; i++) K_to_scL[KL[i]].push_back(scL[i]);
    for (int i = 0; i < NR; i++) K_to_scR[KR[i]].push_back(scR[i]);
    for (long long k = N; k > NL; k--) {
      for (long long sctmp : K_to_scL[k]) scL_que.push(sctmp);
    }
    for (long long k = NL; k >= 1; k--) {
      for (long long sctmp : K_to_scL[k]) scL_que.push(sctmp);
      if (!scL_que.empty()) {
        ans += scL_que.top();
        scL_que.pop();
      }
    }
    for (long long k = 1; k < NL; k++) {
      for (long long sctmp : K_to_scR[k]) scR_que.push(sctmp);
    }
    for (long long k = NL; k < N; k++) {
      for (long long sctmp : K_to_scR[k]) scR_que.push(sctmp);
      if (!scR_que.empty()) {
        ans += scR_que.top();
        scR_que.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
