#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int mxn = 1e5 + 10;
vector<pair<int, int> > adj[mxn];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) adj[i].clear();
    vector<pair<int, pair<int, int> > > camels;
    for (int i = 0; i < n; i++) {
      int k, l, r;
      pair<int, pair<int, int> > tmp;
      cin >> k >> l >> r;
      tmp.first = k;
      tmp.second = {l, r};
      camels.push_back(tmp);
    }
    for (auto x : camels)
      if (x.second.first >= x.second.second) adj[x.first].push_back(x.second);
    multiset<int> Set;
    ll added = 0ll;
    for (int i = 1; i <= n; i++) {
      for (auto x : adj[i]) {
        Set.insert(x.first - x.second);
        assert(x.first - x.second >= 0);
        added += 1ll * (x.first - x.second);
      }
      while (i < (int)Set.size()) {
        added -= 1ll * (*Set.begin());
        Set.erase(Set.begin());
      }
    }
    for (int i = 1; i <= n; i++) adj[i].clear();
    for (auto x : camels)
      if (x.second.first < x.second.second)
        adj[x.first + 1].push_back(x.second);
    Set.clear();
    ll addedmore = 0ll;
    for (int i = n + 1; i >= 2; i--) {
      for (auto x : adj[i]) {
        Set.insert(x.second - x.first);
        assert(x.second - x.first > 0);
        addedmore += 1ll * (x.second - x.first);
      }
      while (n + 1 - i < (int)Set.size()) {
        addedmore -= 1ll * (*Set.begin());
        Set.erase(Set.begin());
      }
    }
    ll ans = 0;
    for (auto x : camels) ans += 1ll * min(x.second.first, x.second.second);
    ans += added;
    ans += addedmore;
    cout << ans << '\n';
  }
  return 0;
}
