#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    vector<int> k(n);
    vector<int> l(n);
    vector<int> r(n);
    vector<tuple<int, int, int> > a;
    vector<tuple<int, int, int> > b;
    for (int i = 0; i < n; ++i) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l >= r) {
        a.emplace_back(k, l, r);
      } else {
        b.emplace_back(n - k, r, l);
      }
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    auto solve = [&](const vector<tuple<int, int, int> > a) {
      priority_queue<int, vector<int>, greater<int> > q;
      long long ans = 0;
      for (int i = 0; i < a.size(); ++i) {
        auto [k, l, r] = a[i];
        if (k > i) {
          ans += l;
          q.push(l - r);
        } else {
          if (q.empty()) {
            ans += r;
          } else if (l - r > q.top()) {
            ans -= q.top();
            ans += l;
            q.pop();
            q.push(l - r);
          } else {
            ans += r;
          }
        }
      }
      return ans;
    };
    cout << solve(a) + solve(b) << '\n';
  }
  return 0;
}
