#include <bits/stdc++.h>
using namespace std;
struct Camel {
  long long k, l, r;
};
bool cmp(Camel &a, Camel &b) { return a.k < b.k; }
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    Camel a[n];
    vector<Camel> l, r;
    long long ans = 0;
    for (long long i = 0; i < n; i++) {
      cin >> a[i].k >> a[i].l >> a[i].r;
      if (a[i].l > a[i].r)
        l.push_back(a[i]);
      else if (a[i].l < a[i].r)
        r.push_back(a[i]);
      else
        ans += a[i].l;
    }
    sort(l.begin(), l.end(), cmp);
    multiset<pair<long long, long long> > taken;
    for (auto &tt : l) {
      if (taken.size() >= tt.k) {
        pair<long long, long long> top = *taken.begin();
        if (top.first < tt.l) {
          taken.erase(taken.begin());
          ans -= top.first;
          ans -= top.second;
          ans += tt.l;
          taken.insert({tt.l, -tt.r});
        } else
          ans += tt.r;
      } else {
        ans += tt.l;
        taken.insert({tt.l, -tt.r});
      }
    }
    taken.clear();
    sort(r.begin(), r.end(), cmp);
    reverse(r.begin(), r.end());
    for (auto &tt : r) {
      long long nk = n - tt.k;
      if (taken.size() >= nk) {
        pair<long long, long long> top = *taken.begin();
        if (top.first < tt.r) {
          taken.erase(taken.begin());
          ans -= top.first;
          ans -= top.second;
          ans += tt.r;
          taken.insert({tt.r, -tt.l});
        } else
          ans += tt.l;
      } else {
        ans += tt.r;
        taken.insert({tt.r, -tt.l});
      }
    }
    cout << ans << endl;
  }
}
