#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using TP = tuple<ll, ll, ll, ll>;
void init_io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
ll calc(vector<vector<ll>> &l) {
  ll n = l.size();
  ll res = 0;
  priority_queue<ll> que;
  for (int i = 0; i < n; i++) {
    for (auto v : l[i]) {
      que.push(v);
    }
    while ((ll)que.size() > i + 1) {
      que.pop();
    }
  }
  while (!que.empty()) {
    res += que.top();
    que.pop();
  }
  return res;
}
void solve() {
  ll n;
  cin >> n;
  vector<ll> k(n), l(n), r(n);
  vector<vector<ll>> le(n), ri(n);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> k[i] >> l[i] >> r[i];
    k[i]--;
    ans += min(l[i], r[i]);
    if (l[i] >= r[i]) {
      le[k[i]].push_back(l[i] - r[i]);
    } else {
      if (k[i] != n - 1) {
        ri[n - 1 - k[i] - 1].push_back(r[i] - l[i]);
      }
    }
  }
  ans += calc(le);
  ans += calc(ri);
  cout << ans << endl;
}
signed main() {
  init_io();
  ll t;
  cin >> t;
  while (t--) solve();
}
