#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int tt = 0; tt < t; tt++) {
    int n;
    cin >> n;
    long long int score = 0;
    int a = 0, b = 0;
    vector<vector<long long int>> camel1(n + 1), camel2(n + 1);
    for (int i = 0; i < n; i++) {
      int k;
      long long int l, r;
      cin >> k >> l >> r;
      score += min(l, r);
      if (l >= r) {
        camel1[k].push_back(l - r);
        a += 1;
      }
      if (l < r) {
        camel2[n - k].push_back(r - l);
        b += 1;
      }
    }
    priority_queue<long long int> pq1;
    for (int j = n; j >= a; j--) {
      for (long long int c : camel1[j]) pq1.push(c);
    }
    if (pq1.empty() == false) {
      score += pq1.top();
      pq1.pop();
    }
    for (int j = a - 1; j >= 1; j--) {
      for (long long int c : camel1[j]) pq1.push(c);
      if (pq1.empty() == false) {
        score += pq1.top();
        pq1.pop();
      }
    }
    priority_queue<long long int> pq2;
    for (int j = 1; j <= a + 1; j++) {
      for (long long int c : camel2[j]) pq2.push(c);
    }
    if (pq2.empty() == false) {
      score += pq2.top();
      pq2.pop();
    }
    for (int j = a + 2; j <= n; j++) {
      for (long long int c : camel2[j]) pq2.push(c);
      if (pq2.empty() == false) {
        score += pq2.top();
        pq2.pop();
      }
    }
    cout << score << endl;
  }
  return 0;
}
