#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long k[n], l[n], r[n];
    vector<pair<long long, long long> > vl, vr;
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      cin >> k[i] >> l[i] >> r[i];
      --k[i];
      ans += l[i];
      if (k[i] == n - 1) continue;
      if (l[i] < r[i])
        vr.push_back({r[i] - l[i], i});
      else if (l[i] > r[i]) {
        vl.push_back({l[i] - r[i], i});
        ans += r[i] - l[i];
      }
    }
    sort(vl.begin(), vl.end());
    set<int>::iterator it;
    set<int> s;
    for (int i = 0; i < n; i++) s.insert(i);
    for (int i = vl.size() - 1; i >= 0; i--) {
      int idx = vl[i].second;
      it = s.upper_bound(k[idx] + 1);
      if (it == s.begin()) continue;
      --it;
      s.erase(it);
      ans += vl[i].first;
    }
    sort(vr.begin(), vr.end());
    s.clear();
    for (int i = 0; i < n; i++) s.insert(i);
    for (int i = vr.size() - 1; i >= 0; i--) {
      int idx = vr[i].second;
      it = s.lower_bound(k[idx] + 1);
      if (it == s.end()) continue;
      s.erase(it);
      ans += vr[i].first;
    }
    cout << ans << "\n";
  }
}
