#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    set<int> vis;
    for (int i = 1; i <= n; i++) vis.insert(i);
    long long ans = 0;
    vector<pair<int, int> > dp1, dp2;
    for (int i = 1; i <= n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      dp1.emplace_back(make_pair(l - r, k));
      ans += r;
    }
    sort(dp1.begin(), dp1.end());
    reverse(dp1.begin(), dp1.end());
    for (int i = 0; i < dp1.size(); i++) {
      int val = dp1[i].first, k = dp1[i].second;
      if (val < 0) {
        dp2.emplace_back(make_pair(val, k));
      } else {
        if (vis.upper_bound(k) == vis.end())
          dp2.emplace_back(make_pair(val, k));
        else {
          auto it = (--(vis.upper_bound(k)));
          vis.erase(it);
          ans += val;
        }
      }
    }
    sort(dp2.begin(), dp2.end());
    for (int i = 0; i < dp2.size(); i++) {
      int val = dp2[i].first, k = dp2[i].second;
      if (vis.upper_bound(k) == vis.end())
        ans += val;
      else {
        auto it = vis.upper_bound(k);
        vis.erase(it);
      }
    }
    printf("%lld\n", ans);
  }
}
