#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1e18;
int main() {
  long long t;
  cin >> t;
  for (long long cs = 0; cs < (t); ++cs) {
    long long n;
    cin >> n;
    long long ans = 0;
    vector<long long> k(n), l(n), r(n);
    for (long long i = 0; i < (n); ++i) cin >> k[i] >> l[i] >> r[i];
    priority_queue<pair<pair<long long, long long>, long long>,
                   vector<pair<pair<long long, long long>, long long>>,
                   greater<pair<pair<long long, long long>, long long>>>
        pshime;
    priority_queue<pair<pair<long long, long long>, long long>> mshime;
    priority_queue<pair<long long, long long>,
                   vector<pair<long long, long long>>,
                   greater<pair<long long, long long>>>
        pfill, mfill;
    for (long long i = 0; i < (n); ++i) {
      if (l[i] >= r[i]) {
        long long x = l[i] - r[i];
        pair<long long, long long> tmp = make_pair(k[i], x);
        pair<pair<long long, long long>, long long> now = make_pair(tmp, i);
        pshime.push(now);
      } else {
        pair<pair<long long, long long>, long long> now = {{k[i], l[i] - r[i]},
                                                           i};
        mshime.emplace(now);
      }
    }
    while (!pshime.empty()) {
      auto t = pshime.top();
      pshime.pop();
      long long shime = t.first.first;
      long long sa = t.first.second;
      long long id = t.second;
      if (shime > (long long)pfill.size()) {
        pfill.push(make_pair(sa, id));
        ans += l[id];
      } else {
        if (pfill.empty()) {
          pfill.push({sa, id});
          ans += r[id];
        } else {
          auto v = pfill.top();
          if (v.first >= sa) {
            ans += r[id];
          } else {
            ans -= v.first;
            pfill.pop();
            pfill.emplace(make_pair(sa, id));
            ans += l[id];
          }
        }
      }
    }
    while (!mshime.empty()) {
      auto t = mshime.top();
      mshime.pop();
      long long shime = t.first.first;
      long long sa = -t.first.second;
      long long id = t.second;
      if (shime + 1 <= n - (long long)mfill.size()) {
        mfill.push({sa, id});
        ans += r[id];
      } else {
        if (mfill.empty()) {
          mfill.push({sa, id});
          ans += l[id];
        } else {
          auto v = mfill.top();
          if (v.first >= sa) {
            ans += l[id];
          } else {
            ans -= v.first;
            mfill.pop();
            mfill.push({sa, id});
            ans += r[id];
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
