#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int i = 0; i < T; i++) {
    int N;
    cin >> N;
    vector<int> K(N);
    vector<long long> L(N), R(N);
    for (int j = 0; j < N; j++) {
      cin >> K[j] >> L[j] >> R[j];
      K[j]--;
    }
    long long ans = 0;
    for (int j = 0; j < N; j++) {
      long long tmp = min(L[j], R[j]);
      ans += tmp;
      L[j] -= tmp;
      R[j] -= tmp;
    }
    vector<pair<int, long long>> A;
    for (int j = 0; j < N; j++) {
      if (R[j] == 0) {
        A.push_back(make_pair(K[j], L[j]));
      }
    }
    int Acnt = A.size();
    vector<vector<int>> idA(Acnt);
    for (int j = 0; j < Acnt; j++) {
      if (A[j].first < Acnt) {
        idA[A[j].first].push_back(j);
      }
    }
    set<tuple<int, long long, int>> stA;
    for (int j = 0; j < Acnt; j++) {
      if (A[j].first < Acnt) {
        stA.insert(make_tuple(1, A[j].second, j));
      } else {
        stA.insert(make_tuple(0, A[j].second, j));
      }
    }
    for (int j = Acnt - 1; j >= 0; j--) {
      for (int id : idA[j]) {
        auto itr = stA.lower_bound(make_tuple(1, A[id].second, id));
        if (itr != stA.end()) {
          if (get<2>(*itr) == id) {
            stA.erase(itr);
            stA.insert(make_tuple(0, A[id].second, id));
          }
        }
      }
      if (get<0>(*stA.begin()) == 0) {
        ans += get<1>(*stA.begin());
      }
      stA.erase(stA.begin());
    }
    vector<pair<int, long long>> B;
    for (int j = 0; j < N; j++) {
      if (R[j] > 0) {
        if (K[j] != N - 1) {
          B.push_back(make_pair(N - 2 - K[j], R[j]));
        }
      }
    }
    int Bcnt = B.size();
    vector<vector<int>> idB(Bcnt);
    for (int j = 0; j < Bcnt; j++) {
      if (B[j].first < Bcnt) {
        idB[B[j].first].push_back(j);
      }
    }
    set<tuple<int, long long, int>> stB;
    for (int j = 0; j < Bcnt; j++) {
      if (B[j].first < Bcnt) {
        stB.insert(make_tuple(1, B[j].second, j));
      } else {
        stB.insert(make_tuple(0, B[j].second, j));
      }
    }
    for (int j = Bcnt - 1; j >= 0; j--) {
      for (int id : idB[j]) {
        auto itr = stB.lower_bound(make_tuple(1, B[id].second, id));
        if (itr != stB.end()) {
          if (get<2>(*itr) == id) {
            stB.erase(itr);
            stB.insert(make_tuple(0, B[id].second, id));
          }
        }
      }
      if (get<0>(*stB.begin()) == 0) {
        ans += get<1>(*stB.begin());
      }
      stB.erase(stB.begin());
    }
    cout << ans << endl;
  }
}
