#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T, n;
  cin >> T;
  for (int t = 0; t < T; t++) {
    cin >> n;
    long long K, L, R, Re = 0, v1 = 0, v2 = 0;
    int B[n + 1];
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        Q, Q1;
    for (int i = 0; i <= n; i++) {
      B[i] = 0;
    }
    vector<pair<int, int>> V1[n + 1], V2[n + 1];
    for (int i = 0; i < n; i++) {
      cin >> K >> L >> R;
      K--;
      Re += min(L, R);
      if (L >= R) {
        V2[n - K - 1].push_back({L - R, i});
        Q.push({i, L - R});
        v2++;
      } else {
        if (n - K - 2 >= 0) V1[n - K - 2].push_back({R - L, i});
        Q1.push({i, R - L});
        v1++;
      }
    }
    int k2 = 0, k1 = 0, k2m = 0, k1m = 0;
    for (int i = 0; i < n - v2; i++) {
      for (int y = 0; y < V2[i].size(); y++) {
        k2++;
        B[V2[i][y].second] = 1;
        Re += V2[i][y].first;
      }
    }
    for (int i = n - v2; i < n; i++) {
      k2m++;
      for (int y = 0; y < V2[i].size(); y++) {
        if (B[V2[i][y].second] == 0) {
          k2++;
          B[V2[i][y].second] = 1;
          Re += V2[i][y].first;
        }
      }
      while (Q.size() > 0 && B[Q.top().first] == 1) {
        Q.pop();
      }
      if (k2 < k2m) {
        B[Q.top().first] = -1;
        Q.pop();
        k2++;
      }
    }
    for (int i = v1; i < n; i++) {
      for (int y = 0; y < V1[i].size(); y++) {
        k2++;
        B[V1[i][y].second] = 1;
        Re += V1[i][y].first;
      }
    }
    for (int i = v1 - 1; i >= 0; i--) {
      k1m++;
      for (int y = 0; y < V1[i].size(); y++) {
        if (B[V1[i][y].second] == 0) {
          k1++;
          B[V1[i][y].second] = 1;
          Re += V1[i][y].first;
        }
      }
      while (Q1.size() > 0 && B[Q1.top().first] == 1) {
        Q1.pop();
      }
      if (k1 < k1m) {
        B[Q1.top().first] = -1;
        Q1.pop();
        k1++;
      }
    }
    cout << Re << "\n";
  }
  return 0;
}
