#include <bits/stdc++.h>
using namespace std;
int t;
int main() {
  cin >> t;
  for (int loop = 0; loop < t; loop++) {
    int n;
    cin >> n;
    priority_queue<pair<int, pair<int, int> >,
                   vector<pair<int, pair<int, int> > >,
                   greater<pair<int, pair<int, int> > > >
        q1;
    priority_queue<pair<int, pair<int, int> > > q2;
    for (int i = 0; i < n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      int num = l - r;
      pair<int, int> p(l, r);
      pair<int, pair<int, int> > s(k, p);
      if (num > 0)
        q1.push(s);
      else
        q2.push(s);
    }
    long long int score = 0;
    int cursor = 0;
    priority_queue<pair<int, pair<int, int> >,
                   vector<pair<int, pair<int, int> > >,
                   greater<pair<int, pair<int, int> > > >
        q3;
    priority_queue<pair<int, pair<int, int> > > q4;
    while (!q1.empty()) {
      cursor++;
      pair<int, pair<int, int> > s = q1.top();
      q1.pop();
      pair<int, int> p = s.second;
      int num = p.first - p.second;
      pair<int, pair<int, int> > s2(num, p);
      q3.push(s2);
      if (s.first < cursor) {
        pair<int, pair<int, int> > _s = q3.top();
        q3.pop();
        pair<int, int> _p = _s.second;
        score += _p.second;
      }
    }
    cursor = n;
    while (!q2.empty()) {
      cursor--;
      pair<int, pair<int, int> > s = q2.top();
      q2.pop();
      pair<int, int> p = s.second;
      int num = p.second - p.first;
      pair<int, pair<int, int> > s2(num, p);
      q4.push(s2);
      if (s.first > cursor) {
        pair<int, pair<int, int> > _s = q4.top();
        q4.pop();
        pair<int, int> _p = _s.second;
        score += _p.first;
      }
    }
    while (!q3.empty()) {
      pair<int, pair<int, int> > s = q3.top();
      q3.pop();
      pair<int, int> p = s.second;
      score += p.first;
    }
    while (!q4.empty()) {
      pair<int, pair<int, int> > s = q4.top();
      q4.pop();
      pair<int, int> p = s.second;
      score += p.second;
    }
    cout << score << endl;
  }
}
