#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
inline bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const double PI = 3.1415926535897932;
void solve() {
  ll n;
  ll ans = 0;
  cin >> n;
  vector<ll> k(n), v(n);
  vector<ll> left, right;
  for (int i = 0; i < int(n); i++) {
    ll l, r;
    cin >> k[i] >> l >> r;
    v[i] = l - r;
    ans += l;
    if (v[i] >= 0)
      left.push_back(i);
    else
      right.push_back(i);
  }
  {
    priority_queue<ll> que;
    vector<P> kouho;
    for (int i = 0; i < int(n); i++) {
      if (v[i] >= 0) kouho.push_back({k[i], i});
    }
    if (kouho.size() == 0) goto bou1;
    sort(kouho.begin(), kouho.end());
    for (int i = left.size() - 1; i >= 0; i--) {
      while (!kouho.empty() && kouho[kouho.size() - 1].first > (ll)i) {
        que.push(v[kouho[kouho.size() - 1].second]);
        kouho.pop_back();
      }
      if (!que.empty()) que.pop();
    }
    while (que.empty()) {
      ans -= que.top();
      que.pop();
    }
  }
bou1:;
  {
    priority_queue<ll, vector<ll>, greater<ll>> que;
    vector<P> kouho;
    for (int i = 0; i < int(n); i++) {
      if (v[i] < 0) kouho.push_back({k[i], i});
    }
    if (kouho.size() == 0) goto bou2;
    sort(kouho.rbegin(), kouho.rend());
    for (int i = left.size(); i < n; i++) {
      while (!kouho.empty() && kouho[kouho.size() - 1].first <= i) {
        que.push(v[kouho[kouho.size() - 1].second]);
        kouho.pop_back();
      }
      if (!que.empty()) {
        ans -= que.top();
        que.pop();
      }
    }
  }
bou2:;
  cout << ans << endl;
  return;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  for (int i = 0; i < int(t); i++) {
    solve();
  }
  return 0;
}
