#include <bits/stdc++.h>
using namespace std;
long long f(vector<pair<int, int> > a) {
  long long ans = 0;
  sort(a.begin(), a.end());
  priority_queue<int> q;
  for (auto i : a) {
    q.emplace(i.second);
    ans += i.second;
    while (q.size() > i.first) ans -= q.top(), q.pop();
  }
  return ans;
}
int main() {
  int t, n, k, l, r;
  cin >> t;
  while (t--) {
    cin >> n;
    long long sum = 0;
    vector<pair<int, int> > L, R;
    for (int i = 0; i < n; i++) {
      cin >> k >> l >> r;
      if (l > r)
        L.emplace_back(k, l - r), sum += r;
      else if (r > l)
        R.emplace_back(n - k, r - l), sum += l;
      else
        sum += l;
    }
    cout << sum + f(L) + f(R) << endl;
  }
  return 0;
}
