#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long m) {
  long long res = 1 % m;
  while (y) {
    if (y & 1) res = (res * x) % m;
    x = (x * x) % m;
    y = (y >> 1) % m;
  }
  return res;
}
long long mod_inverse(long long x, long long m) { return power(x, m - 2, m); }
bool comp(pair<long long, pair<long long, long long> > a1,
          pair<long long, pair<long long, long long> > a2) {
  if (a1.first == a2.first)
    return a1.second.first >= a2.second.first;
  else
    return a1.first < a2.first;
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<pair<long long, pair<long long, long long> > > v;
    vector<pair<long long, pair<long long, long long> > > v1;
    for (long long i = 0; i < n; i++) {
      long long k, l, r;
      cin >> k >> l >> r;
      if (l > r)
        v.push_back(make_pair(k, make_pair(l, r)));
      else
        v1.push_back(make_pair(k, make_pair(r, l)));
    }
    sort(v.begin(), v.end(), comp);
    sort(v1.begin(), v1.end(), comp);
    long long cnt = 1;
    long long ans = 0;
    long long i = 0;
    long long j = 0;
    while (i < (long long)v.size() || j < (long long)v1.size()) {
      if (i < (long long)v.size()) {
        if (v[i].first >= cnt) {
          ans += v[i].second.first;
          i++;
          cnt++;
          continue;
        } else {
          ans += v[i].second.second;
          i++;
          cnt++;
          continue;
        }
      }
      if (j < (long long)v1.size()) {
        if (v1[j].first < cnt) {
          ans += v1[j].second.first;
          j++;
          cnt++;
          continue;
        } else {
          ans += v1[j].second.second;
          j++;
          cnt++;
          continue;
        }
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
