#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int i = 0; i < T; i++) {
    int N;
    cin >> N;
    long long ans = 0;
    vector<pair<int, int>> Ls;
    vector<pair<int, int>> Rs;
    for (int i = 0; i < N; i++) {
      int K, L, R;
      cin >> K >> L >> R;
      if (L > R) {
        Ls.push_back(make_pair(K, L - R));
        ans += L;
      }
      if (R >= L) {
        Rs.push_back(make_pair(N - K, R - L));
        ans += R;
      }
    }
    sort(Ls.begin(), Ls.end());
    sort(Rs.begin(), Rs.end());
    int Lsize = Ls.size();
    priority_queue<int> Lq;
    int Rsize = Rs.size();
    priority_queue<int> Rq;
    for (int i = 0; i < Lsize; i++) {
      pair<int, int> p = Ls[i];
      Lq.push(-p.second);
      if (p.first < i + 1) {
        int t = Lq.top();
        Lq.pop();
        ans += t;
      }
    }
    for (int i = 0; i < Rsize; i++) {
      pair<int, int> p = Rs[i];
      Rq.push(-p.second);
      if (p.first < i + 1) {
        int t = Rq.top();
        Rq.pop();
        ans += t;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
