#include <bits/stdc++.h>
int main() {
  using namespace std;
  unsigned long T;
  cin >> T;
  const auto& solve = [](unsigned long N) {
    vector<pair<unsigned long, unsigned long>> f, b;
    f.reserve(N);
    unsigned long ans{0};
    for (unsigned long i{0}, k, l, r; i < N; ++i) {
      cin >> k >> l >> r;
      if (l > r) {
        ans += r;
        f.emplace_back(k, l - r);
      } else {
        ans += l;
        b.emplace_back(k, r - l);
      }
    }
    sort(begin(f), end(f));
    sort(begin(b), end(b));
    for (unsigned long i{0}; i < f.size(); ++i)
      ans += f[i].second * (f[i].first > i);
    for (unsigned long i{0}; i < b.size(); ++i)
      ans += b[i].second * (b[i].first <= i + f.size());
    cout << ans << endl;
  };
  for (unsigned long iter{0}, N; iter < T; ++iter) {
    cin >> N;
    solve(N);
  }
  return 0;
}
