#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;
using PP = pair<ll, P>;
const int MOD = 1e9 + 7;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<PP> pre, suf;
    for (int i = 0; (i) < (int)(n); i++) {
      ll k, l, r;
      cin >> k >> l >> r;
      if (l > r) {
        pre.push_back(PP(k - 1, P(l, r)));
      } else {
        suf.push_back(PP(k - 1, P(r, l)));
      }
    }
    sort((pre).begin(), (pre).end());
    sort((suf).begin(), (suf).end(), greater<PP>());
    ll res = 0;
    for (int i = 0; (i) < (int)((int)pre.size()); i++) {
      ll kn = pre[i].first;
      ll ln = pre[i].second.first, rn = pre[i].second.second;
      if (kn >= i)
        res += ln;
      else
        res += rn;
    }
    int ps = pre.size();
    for (int i = 0; (i) < (int)((int)suf.size()); i++) {
      ll kn = suf[i].first, rn = suf[i].second.first, ln = suf[i].second.second;
      if (kn < ps + i)
        res += rn;
      else
        res += ln;
    }
    cout << res << endl;
  }
}
