#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    ll mn = 0;
    vector<pair<int, ll>> x, y;
    for (int i = 0; i < n; i++) {
      ll k, l, r;
      cin >> k >> l >> r;
      k--;
      mn += min(l, r);
      if (l >= r) {
        l -= r;
        x.emplace_back(k, l);
      } else {
        r -= l;
        y.emplace_back(n - 2 - k, r);
      }
    }
    sort(x.begin(), x.end());
    sort(y.begin(), y.end());
    multiset<ll> s, t;
    for (int i = 0; i < x.size(); i++) {
      if (x[i].first >= i) {
        s.emplace(x[i].second);
      } else {
        if (s.empty()) {
          continue;
        }
        auto it = s.begin();
        if (*it < x[i].second) {
          s.erase(it);
          s.emplace(x[i].second);
        }
      }
    }
    for (int i = 0; i < y.size(); i++) {
      if (y[i].first >= i) {
        t.emplace(y[i].second);
      } else {
        if (t.empty()) {
          continue;
        }
        auto it = t.begin();
        if (*it < y[i].second) {
          t.erase(it);
          t.emplace(y[i].second);
        }
      }
    }
    for (auto i : s) {
      mn += i;
    }
    for (auto i : t) {
      mn += i;
    }
    cout << mn << '\n';
  }
  return 0;
}
