#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  long long ans = 0;
  vector<vector<pair<long long, long long>>> p(n + 1);
  set<pair<long long, long long>> left, right;
  for (long long i = 0; i < n; ++i) {
    long long k, l, r;
    cin >> k >> l >> r;
    ans += r;
    l -= r;
    p[k].push_back({l, i});
    left.insert({l, i});
  }
  for (long long i = n; i >= 1; --i) {
    for (auto e : p[i]) {
      if (left.count(e)) {
        right.insert(e);
        left.erase(e);
      }
    }
    if (left.size() && left.begin()->first < 0) {
      left.erase(left.begin());
    } else if (right.size()) {
      ans += right.begin()->first;
      right.erase(right.begin());
    } else {
      left.erase(left.begin());
    }
  }
  assert(right.size() == left.size() && right.size() == 0);
  cout << ans << '\n';
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long tc;
  cin >> tc;
  while (tc--) {
    solve();
  }
}
