// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 07/11/20 19:53:47
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  int t; cin>>t;
  while (t--) {
    int n; cin>>n;
    struct Rakuda {
      int k,l,r;
    };
    ll ans = 0;
    vector<Rakuda> lrs, rrs, ers;
    for (int i = 0; i < n; i++) {
      int k,l,r; cin>>k>>l>>r;
      if (l > r)
        lrs.push_back({k-1,l,r});
      else if (l < r)
        rrs.push_back({k-1,l,r});
      else
        ans += l;
    }

    sort(lrs.begin(), lrs.end(), [](auto l, auto r) {return l.k!=r.k ? l.k<r.k : l.l>r.l;});
    int i = 0;
    int d = 0;
    while (d < n and i < lrs.size()) {
      while (i < lrs.size() and d > lrs[i].k) {
        ans += lrs[i].r;
        i++;
      }
      if (i < lrs.size())
        ans += lrs[i].l;
      i++;
      d++;
    }

    sort(rrs.begin(), rrs.end(), [](auto l, auto r) {return l.k!=r.k? l.k<r.k: l.r<r.r;});
    i = rrs.size()-1;
    d = n-1;
    while (d >= 0 and i >= 0) {
      while (i >= 0 and d <= rrs[i].k) {
        ans += rrs[i].l;
        i--;
      }
      if (i >= 0)
        ans += rrs[i].r;
      i--;
      d--;
    }

    cout << ans << endl;
  }

  return 0;
}
