#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int inf = 1e9 + 7;
constexpr ll longinf = 1LL << 60;
constexpr ll mod = 1e9 + 7;
void solve() {
  int n;
  cin >> n;
  ll ans = 0;
  vector<pair<int, int>> l, r;
  for (int i = (int)(0); i < (int)(n); ++i) {
    int k, x, y;
    cin >> k >> x >> y;
    if (x >= y) {
      ans += y;
      l.emplace_back(x - y, k - 1);
    } else {
      ans += x;
      r.emplace_back(y - x, k - 1);
    }
  }
  set<int> L, R;
  for (int i = (int)(0); i < (int)(l.size()); ++i) L.insert(i);
  for (int i = (int)(0); i < (int)(r.size()); ++i) R.insert(i + l.size());
  sort(l.rbegin(), l.rend());
  sort(r.rbegin(), r.rend());
  for (auto e : l) {
    auto itr = L.upper_bound(e.second);
    if (itr != L.begin()) {
      --itr;
      ans += e.first;
      L.erase(itr);
    }
  }
  for (auto e : r) {
    auto itr = R.upper_bound(e.second);
    if (itr != R.end()) {
      ans += e.first;
      L.erase(itr);
    }
  }
  cout << ans << endl;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
