#include <bits/stdc++.h>
using namespace std;
using Z = long long;
Z A = 0, q, n, S = 0, l, r;
Z G(vector<pair<Z, Z>> a) {
  sort(a.begin(), a.end());
  A = 0;
  priority_queue<Z, vector<Z>, greater<Z>> Q;
  for (auto [k, d] : a) {
    Q.emplace(d);
    A += d;
    while (Q.size() > k) A -= Q.top(), Q.pop();
  }
  return A;
}
int main() {
  cin >> q;
  for (int _ = 0; _ < (q); _++) {
    cin >> n;
    vector<pair<Z, Z>> L, R;
    for (int i = 0; i < (n); i++) {
      Z k;
      cin >> k >> l >> r;
      if (l > r)
        L.emplace_back(k, l - r), S += r;
      else if (r > l)
        R.emplace_back(n - k, r - l), S += l;
      else
        S += l;
    }
    cout << S + G(L) + G(R) << endl;
  }
}
