#include <bits/stdc++.h>
using namespace std;
using vec_int = vector<long long>;
using P = pair<long long, long long>;
using Tu = tuple<long long, long long, long long>;
using ll = long long;
long long charToInt(char c) {
  char zero_num = '0';
  return (long long)c - (long long)zero_num;
}
signed main() {
  long long T;
  cin >> T;
  for (long long problem = 0; problem < T; problem++) {
    long long N;
    cin >> N;
    vec_int K(N);
    vec_int L(N);
    vec_int R(N);
    vec_int diff(N);
    long long N1 = 0;
    long long N2 = 0;
    for (long long i = 0; i < N; i++) {
      cin >> K.at(i) >> L.at(i) >> R.at(i);
      diff.at(i) = L.at(i) - R.at(i);
      if (diff.at(i) >= 0) {
        N1++;
      } else {
        N2++;
      }
    }
    vector<Tu> vec_T1(N1);
    vector<Tu> vec_T2(N2);
    long long index1 = 0;
    long long index2 = 0;
    for (long long i = 0; i < N; i++) {
      if (diff.at(i) >= 0) {
        vec_T1.at(index1) = make_tuple(K.at(i), diff.at(i), L.at(i));
        index1++;
      } else {
        vec_T2.at(index2) = make_tuple(N - K.at(i), -1 * diff.at(i), R.at(i));
        index2++;
      }
    }
    sort(vec_T1.begin(), vec_T1.end());
    sort(vec_T2.begin(), vec_T2.end());
    priority_queue<P> pq1;
    priority_queue<P> pq2;
    long long tot_score = 0;
    for (long long i = 0; i < N1; i++) {
      Tu temp = vec_T1.at(i);
      long long K, diff, score;
      tie(K, diff, score) = temp;
      if (pq1.size() < K) {
        pq1.push(make_pair(diff, score));
      } else {
        long long min, sc;
        tie(min, sc) = pq1.top();
        if (min < diff) {
          pq1.pop();
          pq1.push(make_pair(diff, score));
          tot_score += sc - min;
        } else {
          tot_score += score - diff;
        }
      }
    }
    for (long long i = 0; i < N2; i++) {
      Tu temp = vec_T2.at(i);
      long long K, diff, score;
      tie(K, diff, score) = temp;
      if (K == 0) {
        tot_score += score - diff;
        continue;
      }
      if (pq2.size() < K) {
        pq2.push(make_pair(diff, score));
      } else {
        long long min, sc;
        tie(min, sc) = pq2.top();
        if (min < diff) {
          pq2.pop();
          pq2.push(make_pair(diff, score));
          tot_score += sc - min;
        } else {
          tot_score += score - diff;
        }
      }
    }
    while (pq1.size() > 0) {
      long long diff, sc;
      tie(diff, sc) = pq1.top();
      pq1.pop();
      tot_score += sc;
    }
    while (pq2.size() > 0) {
      long long diff, sc;
      tie(diff, sc) = pq2.top();
      pq2.pop();
      tot_score += sc;
    }
    cout << tot_score << endl;
  }
  return 0;
}
