#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long T;
  cin >> T;
  for (long long _ = 0; _ < (T); ++_) {
    long long N, a, b, c;
    long long Ans = 0;
    vector<pair<long long, long long>> KL, KR;
    cin >> N;
    for (long long i = 0; i < (N); ++i) {
      cin >> a >> b >> c;
      if (b >= c) {
        KL.push_back({a, b - c});
        Ans += c;
      } else {
        KR.push_back({N - a, c - b});
        Ans += b;
      }
    }
    sort(KL.begin(), KL.end());
    sort(KR.begin(), KR.end());
    priority_queue<long long> Q;
    long long p = 0;
    for (long long k = 0; k < (N + 1); ++k) {
      while (p < KL.size() && KL[p].first <= k) {
        Q.push(KL[p].second);
        p++;
      }
      while (Q.size() > k) {
        Q.pop();
      }
    }
    while (!Q.empty()) {
      Ans += Q.top();
      Q.pop();
    }
    p = 0;
    for (long long k = 0; k < (N + 1); ++k) {
      while (p < KR.size() && KR[p].first <= k) {
        Q.push(KR[p].second);
        p++;
      }
      while (Q.size() > k) {
        Q.pop();
      }
    }
    while (!Q.empty()) {
      Ans += Q.top();
      Q.pop();
    }
    cout << Ans << endl;
  }
  return 0;
}
