#include <bits/stdc++.h>
using namespace std;
int const nax = 3e5 + 10;
struct cam {
  int k, l, r;
  bool operator<(const cam &x) const { return k < x.k; }
};
void solve() {
  int n;
  cin >> n;
  vector<cam> lft, rgh;
  for (int i = 0; i < n; ++i) {
    cam v;
    cin >> v.k >> v.l >> v.r;
    if (v.l > v.r)
      lft.push_back(v);
    else
      rgh.push_back(v);
  }
  vector<cam> keep[n + 3];
  for (cam &c : lft) keep[c.k].push_back(c);
  priority_queue<pair<int, cam>> setik;
  long long ans = 0;
  for (int i = 1; i <= int(lft.size()); ++i) {
    for (cam c : keep[i]) setik.emplace(c.l - c.r, c);
    while (int(setik.size() > i)) {
      ans += setik.top().second.r;
      setik.pop();
    }
  }
  for (int i = lft.size() + 1; i <= n; ++i)
    for (cam c : keep[i]) ans += c.l;
  while (!setik.empty()) {
    ans += setik.top().second.l;
    setik.pop();
  }
  for (int i = 1; i <= n; ++i) keep[i].clear();
  for (cam c : rgh) keep[c.k + 1].push_back(c);
  int now = 0;
  for (int i = n; i > int(lft.size()); --i) {
    ++now;
    for (cam c : keep[i]) setik.emplace(c.r - c.l, c);
    while (int(setik.size()) > now) {
      ans += setik.top().second.l;
      setik.pop();
    }
  }
  for (int i = 1; i <= lft.size(); ++i)
    for (cam c : keep[i]) ans += c.r;
  while (!setik.empty()) ans += setik.top().second.r, setik.pop();
  cout << ans << '\n';
}
int main() {
  int T;
  cin.tie(0)->sync_with_stdio(false);
  for (cin >> T; T--;) {
    solve();
  }
}
