#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n;
    cin >> n;
    int k[n];
    long long int l[n], r[n];
    long long int ans = 0;
    vector<pair<int, pair<long long int, long long int> > > left;
    vector<pair<int, pair<long long int, long long int> > > right;
    for (int j = 0; j < n; j++) {
      cin >> k[j] >> l[j] >> r[j];
      k[j]--;
      if (l[j] >= r[j])
        left.push_back(make_pair(k[j], make_pair(-l[j], -r[j])));
      else
        right.push_back(make_pair(-k[j], make_pair(-r[j], -l[j])));
    }
    sort(left.begin(), left.end());
    sort(right.begin(), right.end());
    int count = 0;
    for (auto u : left) {
      if (count <= u.first)
        ans -= u.second.first;
      else
        ans -= u.second.second;
      count++;
    }
    count = n - 1;
    for (auto u : right) {
      if (count > -u.first)
        ans -= u.second.first;
      else
        ans -= u.second.second;
      count--;
    }
    cout << ans << endl;
  }
}
