#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll T;
  cin >> T;
  for (ll i = 0; i < T; ++i) {
    ll N, ans = 0;
    cin >> N;
    vector<vector<ll>> LL(200010, vector<ll>(0, 0)),
        RR(200010, vector<ll>(0, 0));
    for (ll i = 0; i < N; ++i) {
      ll K, L, R;
      cin >> K >> L >> R;
      if (L > R)
        LL[K - 1].push_back(L - R);
      else if (R > L)
        RR[K].push_back(R - L);
      ans += min(R, L);
    }
    ll stock = 0;
    for (ll i = 0; i < N; ++i) {
      stock++;
      sort(begin(LL[i]), end(LL[i]), greater<ll>());
      for (auto v : LL[i]) {
        ans += v;
        if (--stock == 0) break;
      }
    }
    stock = 0;
    for (ll i = 0; i < N; ++i) {
      stock++;
      sort(begin(RR[N - 1 - i]), end(RR[N - 1 - i]), greater<ll>());
      for (auto v : RR[N - 1 - i]) {
        ans += v;
        if (--stock == 0) break;
      }
    }
    cout << ans << endl;
  }
}
